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' Quote 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. Quote 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 Quote 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 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... Quote 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 Quote 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 Any excuse to write something lol.. that one made me laugh. Quote 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 Quote 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.. Quote 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 Quote 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. Quote 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: query_cache_type OFF query_cache_size 0 Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/248620-difference-between-and/#findComment-1276818 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.