note 39648 deleted from function.time by aidan
| From: | aidan@php.net | Date: | Sat, 08 Oct 2005 01:06:30 +0000 |
| Subject: | note 39648 deleted from function.time by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-96634@lists.php.net to get a copy of this message | ||
Note Submitter: twillaert at cereus dot be
----
In response to infiwebcon:
Always try to avoid using a function on where fields in your database queries. This is the reason
you did not see any usage of your indices. Every row has to be fetched, the unix_timestamp()
function has to be applied, and then compared to the value you are looking for. Instead, apply the
functions on the constants you specify in your where clause. The following query would give you the
same result, with much better performance:
SELECT * table_name where start_datetime > from_unixtime('some_value') AND end_datetime
< from_unixtime('another value')
In this case you would have used your indices. Even if you don't have indices you will see a
performance boost. If you have a table with 1000 lines, you'll be calling unix_timestamp() 2000
times using a malformed query. When written correctly it's only 2 times for a table of any
size.
Keep storing your timestamps as datetime because they're easier to query that way.
Note: Some databases support function based indices. Still, this would be a bad place to use them.