Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 925163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:28:40+00:00 2026-05-15T19:28:40+00:00

I have a query which can be expressed 2 different ways with the same

  • 0

I have a query which can be expressed 2 different ways with the same results. Which one is better – for performance or other reasons?

First query:

SELECT post_id FROM posts 
    WHERE post_date BETWEEN '2010-01-01 00:00:00' AND '2010-12-31 23:59:59'

Second query:

SELECT post_id FROM posts
    WHERE YEAR(post_date)=2010

Thanks in advance.


After suggestion for benchmarking I have had some searchs and tests. My tests were not benchmarks because of some problems on my computer but they gave me some idea.

I have tested my 4000 rowed table and there was not an important difference. BETWEEN command was just more 0.01-0.02 sec than YEAR(post_date) at 0.09 total query time. It seems using YEAR(post_date) would be good for both performance and usability.

And I have learned that while searches; if hours or minutes are not so important, BETWEEN could be used like this:

SELECT post_id FROM posts 
    WHERE post_date BETWEEN '2010-01-01' AND '2010-12-31'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:28:40+00:00Added an answer on May 15, 2026 at 7:28 pm

    If you have an index on post_date (which I’d recommend if you want to run this query often), then the BETWEEN query can use it. Once you put a column into a function (YEAR(post_date)), MySQL no longer uses the column’s index, so it has to go through all the rows (that’s called a full table scan).

    Check out the output of EXPLAIN SELECT with your queries (check this tutorial) and see what results you’ll get – if there’s a usable index, you should see the difference clearly.

    Of course, benchmark your code and see for yourself – but in general, using functions in WHERE is slower.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a query which can do the following operation. I have a table
I have following query which take more than a minute to execute, how can
Can anyone help? I have a linq query which is embedded inside a extension
I have a very expensive query which gets executed from php and it can
I have this query which groups the results by ORDER#. SELECT ORDER#, MAX(SHIPDATE -
I have three queries which I would like to consolidate into one query which
I have a query which returns results using 'Case When Then End' from three
I have this query which on executing in my sql command line client executes
I have a query which returns two rows of data as count. I want
I have this query which is a dependant query and taking much execution time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.