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

  • SEARCH
  • Home
  • 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 8314417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:48:48+00:00 2026-06-08T20:48:48+00:00

In situations like this which method or mix of methods performs the quickest? $year

  • 0

In situations like this which method or mix of methods performs the quickest?

$year = db_get_fields("select distinct year from car_cache order by year desc");

Or

$year = db_get_fields("select year from car_cache");
$year = array_unique($year);
sort($year);

I’ve heard the distinct on mysql is a real big performance hit for large queries and this table can have a million rows or more. I wondered what combination of database types, Innodb or MyISAM, would work best too. I know many optimizations are very query dependent. Year is an unsigned number, but other fields are varchar of different lengths I know that may make a difference too. Such as:

$line = db_get_fields("select distinct line from car_cache where year='$postyear' and make='$postmake' order by line desc");

I read that using the new innodb multiple keys method can make queries like this one very very quick. But the distinct and order by clauses are red flags to me.

  • 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-06-08T20:48:50+00:00Added an answer on June 8, 2026 at 8:48 pm

    Have MySQL do as much work as possible. If it isn’t being efficient at what its doing, then things likely aren’t set up correctly (whether it is proper indexing for the query you are trying to run, or settings with sort buffers).

    If you have an index on the year column, then using DISTINCT should be efficient. If you do not, then a full table scan is necessary in order to fetch the distinct rows. If you try to sort out the distinct rows in PHP rather than MySQL, then you transmit (potentially) much more data from MySQL to PHP, and PHP consumes much more memory to store all that data before eliminating the duplicates.

    Here is some sample output from a dev database I have. Also note that this database is on a different server on the network from where the queries are being executed.

    SELECT COUNT(SerialNumber) FROM `readings`;
    > 97698592
    
    SELECT SQL_NO_CACHE DISTINCT `SerialNumber`
    FROM `readings`
    ORDER BY `SerialNumber` DESC
    LIMIT 10000;
    > Fetched 10000 records.  Duration: 0.801 sec, fetched in: 0.082 sec
    
    > EXPLAIN *above_query*
    +----+-------------+----------+-------+---------------+---------+---------+------+------+-----------------------------------------------------------+
    | id | select_type | table    | type  | possible_keys | key     | key_len | ref  | rows | Extra                                                     |
    +----+-------------+----------+-------+---------------+---------+---------+------+------+-----------------------------------------------------------+
    |  1 | SIMPLE      | readings | range | NULL          | PRIMARY | 18      | NULL |   19 | Using index for group-by; Using temporary; Using filesort |
    +----+-------------+----------+-------+---------------+---------+---------+------+------+-----------------------------------------------------------+
    

    If I attempt the same query, except replace the SerialNumber column with one that is non-indexed, then it takes forever to run because MySQL has to examine all 97 million rows.

    Some of the efficiency has to do with how much data you expect to get back. If I slightly modify the above queries to operate on the time column (the timestamp of the reading), then it takes 1 min 40 seconds to get a distinct list of 273,505 times, most of the overhead there is in transferring all the records over the network. So keep in mind the limits on how much data you are getting back, you want to keep that as low as possible for the data you are trying to fetch.

    As for your final query:

    select distinct line from car_cache
    where year='$postyear' and make='$postmake'
    order by line desc
    

    There should be no problem with that either, just make sure you have a compound index on year and make and possibly an index on line.

    On a final note, the engine I am using for the readings table is InnoDB, and my server is: 5.5.23-55-log Percona Server (GPL), Release 25.3 which is a version of MySQL by Percona Inc.

    Hope that helps.

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

Sidebar

Related Questions

Are there situations in which this method would not be called? I'm thinking to
Which is the best practice in this situation? I would like an un-initialized array
Becase I've seen (and used) situations like this: In header.h: class point { public:
I have had several situations when i would like to do that. This could
For years I've been following a great pattern called Target-Action which goes like this:
In my code I have often situations like this: public void MyMethod(string data) {
I have a defined idl file, which looks like this: module Banking { typedef
Eclipse source menu has a generate hashCode / equals method which generates functions like
I have a situation like this. I have scrollpane whose viewportView is a JPanel
i have situation like this: class IData { virtual void get() = 0; virtual

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.