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 8752821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:18:31+00:00 2026-06-13T13:18:31+00:00

We are running MySQL/ISAM database with a following table: create table measurements ( `tm_stamp`

  • 0

We are running MySQL/ISAM database with a following table:

create table measurements (
  `tm_stamp` int(11) NOT NULL DEFAULT '0',
  `fk_channel` int(11) NOT NULL DEFAULT '0',
  `value` int(11) DEFAULT NULL,
  PRIMARY KEY (`tm_stamp`,`fk_channel`)
);

The tm_stamp–fk_channel combination is required unique, hence the compound primary key. Now, for certain irrelevant reason, the database will be migrated to InnoDB engine. Upon googling something about it, i found out that the key will dictate the physical ordering of the data on the disk. 90% of the queries currently go as follows:

SELECT value FROM measurements
WHERE fk_channel=A AND tm_stamp>=B and tm_stamp<=C
ORDER BY tm_stamp ASC

Inserts are 99% in order of tm_stamp, it’s a storage for dataloggers network. The table has low millions of rows but growing steadily. The questions are

  1. Should the sole change of storage engine result in any significant performance change, better or worse?
  2. Does the order of columns in the index matter with regards to the most popular SELECT? This blog suggest something along that line.
  3. Thanks to the nature of clustered index, may we perhaps leave out the ORDER BY clause and gain some performance?
  • 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-13T13:18:32+00:00Added an answer on June 13, 2026 at 1:18 pm

    Edit 1:

    It appears that changing the primary key from

    PRIMARY KEY (`tm_stamp`,`fk_channel`)
    

    to

    PRIMARY KEY (`fk_channel`,`tm_stamp`)
    

    always makes sense, for both MyISAM and InnoDB. See http://sqlfiddle.com/#!2/0aa08/1 for proof this is so.

    Original answer:

    To determine if changing

    PRIMARY KEY (`tm_stamp`,`fk_channel`)
    

    to

    PRIMARY KEY (`fk_channel`,`tm_stamp`)
    

    would improve your query’s performance, you need to determine which field’s values cardinality is higher (which field’s values are more varied). Running

    SELECT COUNT(DISTINCT tm_stamp), COUNT(DISTINCT fk_channel) FROM measurements;
    

    will give you the cardinality of the columns.

    So, to answer your question properly we first need to know: What are the common range of values between B and C? 60? 3,600? 86,400? more?

    For example, let’s say that

    SELECT COUNT(DISTINCT tm_stamp), COUNT(DISTINCT fk_channel) FROM measurements;
    

    returns 32,768 and 256. 32,768 divided by 256 is 128. This tells us that tm_stamp has 128 unique values for every value of fk_channel.

    So if the difference between B and C is usually less than 128, then leave tm_stamp as the first field in the primary key. If 128 or greater, then make fk_channel the first field.

    Another question: Does fk_channel need to be an INT (4 billion unique values, half of which are negative)? If not, then changing fk_channel to TINYINT UNSIGNED (if you have 256 unique values), or SMALLINT UNSIGNED (65536 unique values) would save a lot of time and space.

    For example, let’s say you have 256 maximum possible fk_channel values, and 65,536 possible values, then you could change your schema via:

    create table measurements_new (
      tm_stamp INT UNSIGNED NOT NULL DEFAULT '0',
      fk_channel TINYINT UNSIGNED NOT NULL DEFAULT '0', -- remove UNSIGNED if values can be negative
      value SMALLINT UNSIGNED DEFAULT NULL, -- remove UNSIGNED if values can be negative
      PRIMARY KEY (tm_stamp,fk_channel)
    ) ENGINE=InnoDB
    SELECT
      tm_stamp,
      fk_channel,
      value
    FROM
      measurements
    ORDER BY
      tm_stamp,
      fk_channel;
    
    RENAME TABLE measurements TO measurements_old, measurements_new TO measurements;
    

    This will store the existing data in the new table in PRIMARY KEY order, which will improve performance somewhat.

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

Sidebar

Related Questions

I'm running MySQL 5.5.9 and InnoDB. I try to create a versioned table, where
I am running MySQL server on the server's which has following specifications - Dual
I exported a live MySQL database (running mysql 5.0.45) to a local copy (running
I'm running MySQL 5.1.48 on Windows XP and the following trigger test doesn't work:
I have a database in data center, which is running MySQL, and I want
I'm running MySql in ubuntu 10.10 . I created a table called 'employee' having
We have a MySQL database running on a Linux (Ubuntu) server. We are thinking
I have one table spread across two servers running MySql 4. I need to
I have to generate generic DDL scripts from a running MySQL database (source) that
I'm running MySQL 5.1 and storing data from web logs into a table. There

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.