Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do these queries produce different results?

Tags:

mysql

I have run these two queries that look like they do the same thing, but give different results:

SELECT COUNT(stage1) AS total 
FROM progress_status 
WHERE stage1 = 3 
AND stage2 != 3 AND stage3 !=3 AND stage4 !=3 AND stage5 !=3;

# total = 90

SELECT COUNT(stage1) AS total 
FROM progress_status 
WHERE stage1 = 3 
AND (stage2,stage3,stage4,stage5) != (3,3,3,3)

# total = 314
like image 689
Owen Avatar asked Nov 17 '11 09:11

Owen


People also ask

Why does the same query take different times?

Either you are getting different performance because the system is under a different sort of load, you are getting different performance because of data volume changes, or you are getting different performance because you are getting different query plans.

What is queries how they are different from views?

The view is a query stored in the data dictionary, on which the user can query just like they do on tables. It does not use the physical memory, only the query is stored in the data dictionary. It is computed dynamically, whenever the user performs any query on it.

How do you compare the results of two queries?

Comparing the Results of the Two Queries The solution to this is very simple. Run both queries using a UNION to combine the results! The UNION operator returns unique records. If the two results sets are identical the row count will remain the same as the original query.

How does query affect performance?

Query performance also depends on data volume and transaction concurrency. Executing the same query on a table with millions of records requires more time that performing the same operation on the same table with only thousands of records.


2 Answers

The second one is doing:

AND (stage2 != 3 OR stage3 !=3 OR stage4 !=3 OR stage5 !=3);
like image 151
Ariel Avatar answered Sep 22 '22 16:09

Ariel


It's normal:

  • Your first query asks all stage columns to be different from 3
  • Your second query asks those columns not beeing equal to 3 at the same time, so if one is different from 3 record is taken! This query is like you're using OR in your WHERE clause.
like image 33
Marco Avatar answered Sep 26 '22 16:09

Marco