Query Performance in Mysql

Asked

Viewed 172 times

1

I have in my form the option to perform a search for Rg or Cpf. So I got the question about the performance of the query using the following code:

SELECT * FROM DADOSPESSOAIS WHERE (Rg = @Rg OR Cpf = @Cpf);

This code above "costs" more than if I made two queries, ie, make a query for Rg and if return null I do another for Cpf ?

SELECT * FROM DADOSPESSOAIS WHERE (Rg = @Rg);

SELECT * FROM DADOSPESSOAIS WHERE (Cpf = @Cpf);
  • What is the doubt then?

  • 2

    Depending on the use and context, there are other factors as well as the use of indexes. Who can answer this is the database, use the command explain to find out about the plan for the implementation of the consultation.

2 answers

0

In this case I believe that 1 SELECT is better than 2 Selects.

But what will really make a difference is the existence of an index for the field Rg and another index for the field Cpf.

0

The difference in performance between the two queries are minimal, they are milliseconds thing, but you have to keep in mind how the answer above said "better one queries than two", because two queries besides possibly bring you redundant data to your view, also you would have to make a whole loop of interaction between them until you get the desired result, so whatever way you are working on your system, whenever you are going to present some result on the screen to your users, always try to have it in 1 query (however, we know that it is not always possible), always seeking to use the native syntaxes of your BD to avoid as much as possible that you have to do validations and derivatives on the client part, always seek to already bring the correct data from your server and just take care to show them in your client side.

I hope I’ve helped and beaten.

See you around.

Browser other questions tagged

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