OAFC_Rob Posted October 7, 2011 Share Posted October 7, 2011 What is the difference between the two statement apart from the fact I'm using between and greater than. If there any reason to why one would be prefered, part from terms of length of coding? SELECT * FROM table WHERE date >= '2012-01-01' AND <= '2012-04-01' SELECT * FROM table WHERE date BETWEEN '2012-01-01' AND '2012-04-01' Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/ Share on other sites More sharing options...
AyKay47 Posted October 7, 2011 Share Posted October 7, 2011 performance wise I really do not know if there is a difference.. I would imagine that using the BETWEEN function would be a bit quicker.. however I do not know the infrastructure of the function so its hard to say.. BETWEEN is the preferred method simply because it is there for this purpose and decreases any potential of syntax mistakes etc.. perhaps someone else can shed some more light on the performance side of this question. Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/#findComment-1276753 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 I just ran a very small test, 10000 of each query calling a result set of 10. I wouldnt say its a benchmark test but it does show what its like in a development env. $query = "SELECT * FROM `chas` WHERE `date` >= '2011-10-01' AND `date` <= '2011-10-30'"; // 6.6296169757843 seconds $query2 = "SELECT * FROM `chas` WHERE `date` BETWEEN '2011-10-01' AND '2012-10-30'"; // 5.5761868953705 Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/#findComment-1276762 Share on other sites More sharing options...
AyKay47 Posted October 7, 2011 Share Posted October 7, 2011 Quote I just ran a very small test, 10000 of each query calling a result set of 10. I wouldnt say its a benchmark test but it does show what its like in a development env. $query = "SELECT * FROM `chas` WHERE `date` >= '2011-10-01' AND `date` <= '2011-10-30'"; // 6.6296169757843 seconds $query2 = "SELECT * FROM `chas` WHERE `date` BETWEEN '2011-10-01' AND '2012-10-30'"; // 5.5761868953705 nice.. it appears the the BETWEEN function is an optimized way of writing the lengthened code above,no surprise really, and should always be used.. thank you for the tests/results... Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/#findComment-1276764 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 Any excuse to write something Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/#findComment-1276766 Share on other sites More sharing options...
AyKay47 Posted October 7, 2011 Share Posted October 7, 2011 Quote Any excuse to write something lol.. that one made me laugh. Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/#findComment-1276767 Share on other sites More sharing options...
OAFC_Rob Posted October 7, 2011 Author Share Posted October 7, 2011 haha! I thought that Between would be quicker but there doesn't seem to be much documentation to back one or the other I guess it comes down to personal preference and waht you're actually using it for Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/#findComment-1276781 Share on other sites More sharing options...
AyKay47 Posted October 7, 2011 Share Posted October 7, 2011 as i said before, the BETWEEN function is there for a reason, it is optimized.. use it.. embrace it.. Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/#findComment-1276784 Share on other sites More sharing options...
OAFC_Rob Posted October 7, 2011 Author Share Posted October 7, 2011 I already am Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/#findComment-1276786 Share on other sites More sharing options...
PFMaBiSmAd Posted October 7, 2011 Share Posted October 7, 2011 Due to mysql's query cache, any timing differences seen between repeatedly executing two different queries are in the time taken to send the query statement from php to the mysql server and the parsing of the query statement on the mysql server, because after the first time each of the two different queries is executed, the result set for each specific query statement is held in the query cache. The only thing you can determine from this is that a shorter query statement takes less time to send and to parse. You would need either disable the query cache or cause a change to the table data between each query so that the cache is flushed to actually see any timing difference due to the query being used. Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/#findComment-1276797 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 FYI: Quote query_cache_type OFF query_cache_size 0 Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/#findComment-1276817 Share on other sites More sharing options...
fenway Posted October 7, 2011 Share Posted October 7, 2011 Or, use SQL_NO_CACHE. Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/#findComment-1276818 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.