Query Performance in MySql

1

I have in my form the option to perform a search for Rg or Cpf. Then I came to doubt about query performance 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 I made a query for Rg and if I returned null I would make another one for Cpf?

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

SELECT * FROM DADOSPESSOAIS WHERE (Cpf = @Cpf);
    
asked by anonymous 02.12.2015 / 12:02

2 answers

0

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

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

    
09.05.2016 / 17:17
0

The difference in performance between the two queries is minimal, they are milliseconds long, but you have to keep in mind how the above answer said "better one queries than two" because two queries besides possibly bring you redundant data for your view, you would also have to make an entire loop of interaction between them until you get the desired result, so whatever form 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 it is not always possible), always try to use the native syntax of your DB to avoid as much as possible that you have to do validations and derivatives on the client side, correct your server's data and just have the concern of showing them on your client side.

I hope to have helped and assaulted you.

See you later.

    
22.02.2017 / 15:30