Optimizing a Mysql table with 40 fields

Asked

Viewed 87 times

1

Hello, people! I have a table in Mysql with 40 fields, it is a system of resumes. I am optimizing both the table and the queries performed in PHP.

The point is that to perform searches for specific profiles, 20 of these fields are used, that is, a query is "heavy". When a given field is not used by the user at the time of filling in the search data, it is not included in my query. Anyway, when I went to set up my indexes, I saw that the maximum acceptable is 16 columns. That’s why I’m here to ask your opinion.

Check below some of the fields in this table:

cur_nome
cur_cpf
cur_senha
cur_nascimento
cur_estado_civil
cur_endereco
cur_estado
cur_cidade
cur_area1  (área de interesse 1) -> recebe um id de outra tabela
cur_area2  (área de interesse 2) -> recebe um id de outra tabela
cur_area3  (área de interesse 3) -> recebe um id de outra tabela
cur_habilitacao_a (carteira de habilitacao a) -> Recebe Sim ou Não
cur_habilitacao_b (carteira de habilitacao b) -> Recebe Sim ou Não
cur_habilitacao_c (carteira de habilitacao c) -> Recebe Sim ou Não
cur_habilitacao_d (carteira de habilitacao d) -> Recebe Sim ou Não
cur_habilitacao_e (carteira de habilitacao e) -> Recebe Sim ou Não

Based on the suggestion of a friend in the comments below, I took this table and divided it into three, being as follows:

Tabela1  com 15 campos
Tabela2  com 15 campos
Tabela3  com 10 campos

Table 2 and Table 3 have as foreign key (Table id 1) the primary key itself.

One question I’ve had is this::

Assuming all fields are submitted at once, at the time I save the data, then I would do 3 Inserts in mysql, right?

example:

sql1 = insert into Tabela1 (campos) values (valores);
sql2 = insert into Tabela2 (campos) values (valores);
sql3 = insert into Tabela3 (campos) values (valores);

In this case, to ensure that Table2 and Table3 will receive in their foreign keys, the primary of Table1, it is recommended that I redeem the value of the recorded id of Tabela1, and so make your reference in tables 2 and 3? It would look like this:

 sql1 = insert into Tabela1 (campos) values (valores);
 $idgravado = mysqli_insert_id($conexao);                         

 sql2 = insert into Tabela2 (tab2_codigo, outros_campos) values ($idgravado, valores);
 sql3 = insert into Tabela3 (tab3_codigo, outros_campos) values ($idgravado, valores);

What do you say, folks?

  • Is this just for research? Do you need to have all the fields for research? The question is a bit wide, because it depends on a lot of things. The ideal was not to have so many fields in the search... But if you need, what can be done?

  • I would also, one table per step, and then use Join to search for the pertinent records, it is much better to add, even consult, if you want to consult a record in specific, of a specific step, do not need to search in every table, only seeks the necessary even.

  • Ola, André and Jorge, I made an edition in my question with some doubts about this question of division of tables in more, see above.

  • It seems fair, Jorge knows more of SQL, but I would do the same procedure I told you, and I recover the id of the main table and insert in the other pertinent.

  • I will release in all some 16 fields for research, it is necessary to consult the records through several criteria.

  • I’ll do it then, thanks you guys.

Show 1 more comment

1 answer

1

Dude, the ideal would be for you to create multiple tables for example:

  1. tblDadosPersonals
  2. tblEndereco
  3. tblExperience

So later you could use the INNER JOIN to search for data in other tables with the same index. Read this article.

  • 1

    Hello Cleidimar, Thanks for the clarifications. I will make a modification in my question to remedy some doubts regarding your suggestion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.