-1

I have three query in different php files. But I have had some speed problem.

query1 and query2 are approximately take 650ms.

query3 just takes approximately 140ms.

Is there any query mistake in the following? If it is, please let me inform.

Or is there any different query that has same result and shorter the time.

$query1 = "SELECT * FROM konu WHERE ( NOW() <  DATE_ADD( `time` , INTERVAL 1 DAY) ) ORDER BY time DESC";

$query2 = "SELECT * FROM konu WHERE ( NOW() <  DATE_ADD( `time` , INTERVAL 2 DAY ) ) AND ( NOW() >  DATE_ADD( `time`, INTERVAL 1 DAY ) ) ORDER BY time DESC";

$query3 = "SELECT * FROM konu WHERE ( NOW() >  DATE_ADD( `time`, INTERVAL 2 DAY ) ) AND ( NOW() <  DATE_ADD( `time`, INTERVAL 900 DAY ) ) ORDER BY time DESC";
4

1 回答 1

2

In case you didn't understand the comment from Dagon, and is completely correct. When running a query, if you are modifying a column name via a function such as you are doing with date manipulation, the index on that column can not be used. However, if you alter the NOW(), an existing index on the table based on the 'time' column CAN be used.

SELECT * 
   FROM konu 
   WHERE 
      NOW() <  DATE_ADD( `time` , INTERVAL 1 DAY) 
   ORDER BY 
      `time` DESC";

is the same as

SELECT * 
   FROM konu 
   WHERE 
      `time` > DATE_ADD( NOW(), INTERVAL -1 DAY) 
   ORDER BY 
      `time` DESC";

Is comparing the results as

now LESS THAN   time          Time PLUS 1 day
8/12 10pm       8/12 2am      8/13 2am     keep this
8/12 10pm       8/11 11pm     8/12 11pm    keep this
8/12 10pm       8/10  4pm     8/11 4pm     not this

vs

time AFTER     now            MINUS 1 day
8/12 2am       8/12 10pm      8/11 10pm      keep this
8/11 11pm      8/12 10pm      8/11 10pm      keep this
8/10 4pm       8/12 10pm      8/11 10pm      not this

the time unmodified can utilize an indexe for performance AND order by. You just need to reverse what you are asking for and leave original column alone.

于 2013-08-13T01:56:31.037 回答