Jump to content

DIFFERENCE between AND <= & >=


OAFC_Rob

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.