INDEXING IN mysql when using multiple columns - php

In my project I am using DataTables plugin to display data in table from database.
Every column has filter so user can filter data based on input on each column.
I am confused how to setup indexes, do ever column need index?
Is it good practice to index every column in table?
My table have 6 columns.

Related

How to index normalized SQL database for Elasticsearch

I have already indexed a database (SQL) with a single table which is in sync with its Elasticsearch index. Now I want to index a database with multiple normalized tables. So, how should I index those tables? Should I write multiple JOIN queries in my logstash file during indexing the database tables, or should I index each table one by one and perform multiple index search? But for second way, I do not know how to form elasticsearch query for the relevant SQL queries. I am new to Elasticsearch. So any guidance for the problem would be appreciated. Here I am also attaching the schema of the database. One more thing, I am using PHP client for searching and displaying data.
First of all, everything will depend on how you want to build your indexes in elasticsearch, that is, if you want an index for each table or an index for several tables.
My advice is:
Create a trigger in the database to audit every change (insert, update, delete) and store it in a news table along with the action and a state.
Create a view for each type of novelty or table, this view will depend on how you want to index everything.
Use jdbc to call the views says the state is equal to pending (raw).
Use filters to normalize your data and adjust it to your elasticsearch structure.
Use JDBC output to update the database by setting the newness to processed to prevent it from appearing in the query.
In addition to these points, my recommendation is that you have those tables in a single index, for example, employee, where you can create different nested objects for each entity in the database, such as department, etc. where you could add the code tags and description of each one. You can check if it has not been clear 😀

Storing arrays/lists in one mysql entry

I am working on a project that requires a MySQL database.
The project is a quiz/study site and it needs to store a users login data and then a list of the quizes they have saved, and their score for each question on each quiz they have saved. So say we have user 1. I want to go to a table called "user_content" and then be able to see which quizes they have saved and their scores for those quizes.
The quizes will be made in a unique database, and referenced via a foreign key.
The issue is, I want to have a fixed number of columns in the "user_content" section, so I need just one column that has all the foreign keys for the quizes that a user has saved. I don't want a system where you have column "quiz-1" and "quiz-2" is there a way to just have one column that stores a list or array of all the quiz foreign keys? The same goes for storing the user's scores for their favorite quizes, I want a single column that will store an array/list of all their scores. Is this possible in MySQL and what is the best way to achieve this?
(This is using php, not javascript)
So as in the comments this could be accomplished in a well defined RDBMS structure for example: table for the quiz ID and then line item entries for each quiz foreign key back to the quiz ID table.
To answer your question you can store an array as json into a MySQL database by passing your PHP array into json_encode() then inserting that into the MySQL field. In the long run this could impact performance and flexibility when writing queries. You would have to run json_decode() back in PHP to restore the structure of the array. But this would solve the requirements of the question.

Merging Two Databases - How to Skip Same ID or Generate New ID

I have two MySQL databases. I would like to data from one database to another. Both have the same structure and entries except that one database has same IDs for different items within the same tables. I don't want to replace the data from the old to the new database. If IDs are there, I would like the new database to skip it. If it's a duplication, I would like a new ID to be generated.
I'd like to use phpmyadmin for this but have no idea if this is even possible.
0.) Make backup of both tables
PHPMYADMIN will be sufficient for your request.
First you need to ensure there is no duplicating id's or primary keys.
Assuming two tables testtable1 and testtable2 have columns testtable_id, name
1.) firstly you would make query on second table
UPDATE testtable2 SET testtable2.testtable_id = testtable2.testtable_id + (SELECT MAX( testtable1.testtable_id ) FROM testtable1);
2.) Than, again in testtable2, there is tool Copy table to (database.table): under Operations menu, set DB name and testtable1 name (db name should be already set), select Data only radio button option and click Go. 3.) Now, you have all data from both tables in testtable1.
Edit. Firstly I thought it is matter of two tables in same database. But nevertheless you can use step two for rest of the tables too. Just set correct DB and table name in step two. Also, before that, set query so expecting ID to be higher than MAX ID of table you want to extend. You can hard code parenthesis part with exact number of MAX ID first DB corresponding table.

Can you make/insert tables inside every row in a table of a database in PHP/MySQL?

Is it possible? If yes, how?
I am using xampp for my database and I wanted to make a database with "students" table, only one column for the list of student IDs, and every student ID has another table inside. (I was considering making tables for every student ID so that it can be done directly but it seems that it PHP/MySQL does not allow integers as table name).
No, you cannot put tables inside a row in SQL. That's not compatible with the concept of a relational database. What you can do is provide a foreign key to another table where you can collect the data you need to link to your first table.
What you need is to have one table with all you want about the students, and add a field with their ID.
So, yo don't have to get the information from student #123 as
SELECT * FROM 123
but
SELECT * FROM students_table WHERE IdStudent = 123

Laravel: Split model across two tables

I have a model titles
My titles DB table is getting rather large 100,000+ rows of data.
What is the best way to create a new titles2 table and join it with the titles table so that they can both be the same titles model?
Also if I finish adding rows to titles at id 100,000 should i start incrementing titles2 at id 100,001 to that the ids will always be unique?
You can add another column ex. source to differentiate these two table records. This way you can make sure that there's no way we will run into the problem of mixing these two datasets.
However for conveniency, we still want to make sure the id itself is unique across different dataset, which requires you not to use auto-increment for id field, because you'll gonna be the one to handle the id generation.
Some of the issues that I run into includes using the following line to update or insert the record,
$db->table('users')->insert($record);
instead of
User::create($record); // assume you are using auto-increment ids

Categories