What is the best practice to know if an Row exists in a SELECT in Mysql?

Asked

Viewed 26,385 times

10

For example, when we check if a user is already registered in the table, we do not need any data return by query, just check if the number of Rows is greater than 0 (num_rows > 0). Us scripts and frameworks i see using for this purpose the following consultations:

SELECT * FROM tabela WHERE usuario = 'exemplo';

SELECT usuario FROM tabela WHERE usuario = 'exemplo';

SELECT COUNT(*) FROM tabela WHERE usuario = 'exemplo';

SELECT COUNT(usuario) FROM tabela WHERE usuario = 'exemplo';

if (num_rows > 0)
//existe
else
//não existe

Doubt:

What is the best practice to achieve performance when we need to do a query to know if a value in a column already exists?

  • http://stackoverflow.com/questions/61033/how-to-check-if-a-value-already-exists-to-avoid-duplicates I think you can answer your question here.

  • 2

    COUNT(*) or COUNT(usuario) are expensive because they bring data, in which case I suggest the COUNT(1) or else the SELECT 0 FROM @bigown. And I also suggest trying to compare the bank’s strategies for each query with EXPLAIN SELECT (just add EXPLAIN in front of the query). Finally, try creating an index for the field usuario, I believe it will improve performance.

5 answers

14


You need to see the goal you want, if you really just want to know if there are values in the database the simplest among these options would be:

SELECT 0 FROM tabela WHERE usuario = 'exemplo';

I put in the Github for future reference.

It will make very little difference if you do, but it’s better than all the others because you don’t have to bring any data from the database, you don’t have to do any other operation other than selecting the line.

There is one way to find out. Test with all options and see which one is faster. You can change depending on the version.

This form is great if you want to know how many already exist or if you are sure that there will be 0 or 1 element. But it is bad if there can be several because it will return several lines without need. Then the best would be:

SELECT COUNT(0) FROM tabela WHERE usuario = 'exemplo';

which is basically the same thing

SELECT COUNT(*) FROM tabela WHERE usuario = 'exemplo';

The advantage of this way is that it already returns the number of lines found, so it is guaranteed that the result will only have one line, less traffic generated. And you can read the result of query to know if you have more than 0 lines.

Which is more advantageous, read the result or ask the function to indicate how many lines returned? I don’t know, I’d have to run a test, but I doubt if I can find a difference that would justify choosing one or the other. Essentially it will be the same, it will be derisory near the whole operation, even if it runs this 1 million times.

The only issue in this way is that it will not be able to use the proposed verification code if (num_rows > 0), After all always the number of lines in this query will be 1, since what returns is the count of lines that satisfy the condition and not the lines themselves.

If you can change this check, then it may be interesting to return a boolean indicated the existence of lines that correspond to the condition:

SELECT (SELECT COUNT(*) FROM tabela WHERE usuario = 'exemplo') > 0;

This form returns a boolean indicating if there are lines.

But if the problem is having duplicate, then all the ways are wrong. You can get involved in a running condition and the result will not be reliable.

  • this way he returns a Boolean to say if there is?

  • 1

    No, his goal is not to return this a boolean. It is true that the question is poorly asked and is looking for a difference that will be minimal in most cases, but it is not specified that it should return a boolean, but rather give the chance to know whether it exists or not. I doubt even that I should do it this way, but I won’t get into the merits.

  • Ah, yes! It was my own doubt, I had never seen the expression in this way and I found it interesting. Only one last doubt, if nothing returns, this expression still has some use?

  • Yes, it still returns amount of existing records that satisfy the condition. I just remembered one thing, so it can do better.

  • Ah yes. Thank you!

5

The ideal would be:

SELECT usuario FROM tabela WHERE usuario = 'exemplo';

Considering that the 'user' field will not be repeated (it will be unique or Primary key) and there will be an index created on top of this field, this is the least expensive form for the database.

4

The option LIMIT 1 makes SQL end when it finds the first record, is the fastest way to check if a record exists.

SELECT
  1
FROM
  tabela
WHERE
  usuario = 'exemplo'
LIMIT 1
;
  • And the other duplicate records ignored... it stops at the first.

2

You can try

SELECT EXISTS(SELECT * FROM tabela WHERE usuario = 'exemplo')

or

SELECT EXISTS(SELECT 1 FROM tabela WHERE usuario = 'exemplo')

1

If only to know EXISTE ROW COM usuario = 'exemplo', I recommend using SELECT COUNT(1) FROM tabela where usuario = 'exemplo'. For the count(1) go through the table, check if the condition is met, and skip the other table columns. And when you receive the result just put boolean existe = resultado > 0

Browser other questions tagged

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