[Solved] Strange MySQL behaviour for comparison

EverSQL Database Performance Knowledge Base

Strange MySQL behaviour for comparison

Database type:

I found a question that might explain what I am getting as result. But still it seems an odd behaviour.

We have a table filled with supplier products, it gets updated each day (new data read from an xml). A column 'updated' gets a new datetime value during the process. Products that disappear from the supplier xml file should be marked EOL in the column 'flag'

After the import from the xml file I run following SQL to try and find products which should be marked EOL.

SELECT * FROM `app_supplier_products` WHERE `flag` != 'EOL' AND `supplier_id` = 1 AND `updated` < '2014-12-29 07:15:01'

However this returned no rows. If we altered the time to 07:15:15 we did get results. It seemed as if the smaller than comparison did not return rows from another day.

After doing some other tests we managed to get it working with following SQL:

SELECT * FROM `app_supplier_products` WHERE (`flag` IS NULL OR `flag` = 'MODIFIED') AND `supplier_id` = 1 AND `updated` < '2014-12-29 07:15:01'

The flag column can, at this point, be NULL, MODIFIED or EOL. In the future we might add other possible values. This means that we'll have to update all queries when we add another value.

Why doesn't the first query return the expected rows ?

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `app_supplier_products` ADD INDEX `app_products_idx_supplier_id_flag` (`supplier_id`,`flag`);
The optimized query:
SELECT
        * 
    FROM
        `app_supplier_products` 
    WHERE
        `app_supplier_products`.`flag` != 'EOL' 
        AND `app_supplier_products`.`supplier_id` = 1 
        AND `app_supplier_products`.`updated` < '2014-12-29 07:15:01'

Related Articles



* original question posted on StackOverflow here.