Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6973003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:03:03+00:00 2026-05-27T17:03:03+00:00

Here are the sql commands I’ve done, and I don’t understand why there’s so

  • 0

Here are the sql commands I’ve done, and I don’t understand why there’s so few difference between without the key (~1.45 s), and with the key (~1.15 s):

mysql> show create table devis;
| devis | CREATE TABLE `devis` (
  `id` bigint(20) unsigned NOT NULL auto_increment,
  ...blabla...
  `date_v_creation` datetime default NULL,
  `date_v_fin` datetime default NULL,
  ...blabla...
  `etat` tinyint(4) default NULL,
  `etat_date` datetime default NULL,
  PRIMARY KEY  (`id`),
  KEY `date_v_creation` (`date_v_creation`),
  KEY `date_v_fin` (`date_v_fin`),
  ...blabla...
) ENGINE=InnoDB AUTO_INCREMENT=3714317 DEFAULT CHARSET=utf8 |

mysql> select date_v_creation from devis
where etat=3
and date_v_fin is null
and TO_DAYS(NOW()) - TO_DAYS(date_v_creation) > 54;

+---------------------+
| date_v_creation     |
+---------------------+
| 2011-10-30 21:44:54 | 
| ...blabla...        | 
| 2011-10-30 21:48:05 | 
+---------------------+
216 rows in set (1.45 sec)

mysql> alter table devis add key( date_v_fin, etat);

mysql> select date_v_creation from devis
where etat=3
and date_v_fin is null
and TO_DAYS(NOW()) - TO_DAYS(date_v_creation) > 54;

+---------------------+
| date_v_creation     |
+---------------------+
| 2011-10-30 21:44:54 | 
| ...blabla...        | 
| 2011-10-30 21:48:05 | 
+---------------------+
216 rows in set (1.13 sec)

If I do it a lot of times, it’s always between (1.05 sec) and (1.15 sec).

There have been a lot of times where I’ve added keys to have a shorter response time, and when it didn’t work, the time didn’t change, and when it worked, the time change to a factor of 10 to the least.

Am I doing something wrong, missing something or is this normal t gain so few?

Thanks a lot!

Update

Here’s the query that solved my problem but I still don’t get why it solved it.

select date_v_creation
from devis
where etat=3
  and date_v_creation < NOW() - INTERVAL 54 DAY
  and date_v_fin is null;   

Here’s the explain of my “bad” query:

mysql> EXPLAIN select date_v_creation from devis where etat=3 and date_v_fin is null and TO_DAYS(NOW()) - TO_DAYS(date_v_creation) > 54;
+----+-------------+-------+------+-------------------------+------------+---------+-------+-------+-------------+
| id | select_type | table | type | possible_keys           | key        | key_len | ref   | rows  | Extra       |
+----+-------------+-------+------+-------------------------+------------+---------+-------+-------+-------------+
|  1 | SIMPLE      | devis | ref  | date_v_fin,date_v_fin_2 | date_v_fin | 9       | const | 15913 | Using where |
+----+-------------+-------+------+-------------------------+------------+---------+-------+-------+-------------+
1 row in set (0.34 sec)

Here’s the explain of the “good”, optimized query:

mysql> explain select date_v_creation from devis where etat=3   and date_v_creation < NOW() - INTERVAL 54 DAY   and date_v_fin is null;
+----+-------------+-------+-------+-----------------------------------------+-----------------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys                           | key             | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+-----------------------------------------+-----------------+---------+------+------+-------------+
|  1 | SIMPLE      | devis | range | date_v_fin,date_v_creation,date_v_fin_2 | date_v_creation | 9       | NULL | 1458 | Using where |
+----+-------------+-------+-------+-----------------------------------------+-----------------+---------+------+------+-------------+
1 row in set (0.00 sec)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:03:04+00:00Added an answer on May 27, 2026 at 5:03 pm

    You have basically two problems:

    1. There is no useful key for the query: useful here is the key that includes index on the field with equation, and then with the comparison. That is you need a key on (etat, date_v_creation)
    2. the index on date_v_creation currently can not be used, because the comparison is on a function of the field value instead of the value itself. That’s why the key on date_v_creation did not help. SO you should rewrite the query as follows:

      select date_v_creation
      from devis
      where etat=3
        and date_v_creation < NOW() - INTERVAL 54 DAY
        and date_v_fin is null;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a question for you mysql + python folks out there. Why does this
I've got this code here: SqlCommand CodeStatus = new SqlCommand(SQL, DB); DB.Open(); Reader =
Help me settle an argument here. Is this: SqlCommand cmd = new SqlCommand( sql
I am new to SQL Server 2008 fill factor, as mentioned here in SQL
Here is my SQL Statement which is not returning DISTINCT Thread Titles. SELECT DISTINCT
Have other people here played with SQL Server 2008 Compression at either the page
Replaces Question: Update multiple rows into SQL table Here's a Code Snippet to update
After reading a couple of answers and comments on some SQL questions here, and
Querying a Nested Set Model table , here's the SQL... how can this be
Here's my current SQL statement: SEARCH_ALBUMS_SQL = SELECT * FROM albums WHERE title LIKE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.