473,394 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How do I create a clustered index and use it to speed up my MySQL query

I have a very slow SQL statement where I am trying to grab a chunk of data for a given time/date frame from one table. My table is getting quite large and the query is taking too long. Sometimes 10 seconds to grab just 12 data points. I came across this earlier post:

http://bytes.com/topic/sql-server/an...slow-sql-query

and it seems like if I do some smart indexing then, I can solve my problem. I am very new to the world of databases and don't know how to create a clustered index and secondly how to use one.

Below is my query.

Expand|Select|Wrap|Line Numbers
  1. SELECT collect_time, sensor_id, value 
  2. FROM scalar_value_measurements 
  3. WHERE sensor_id = 254 
  4. AND collect_time BETWEEN '2012-12-03 07:52:57' 
  5. AND NOW() 
  6. ORDER BY collect_time ASC
I've tried taking out the ORDER BY, but it does not speed things up.

Below is the MySQL statement used to create the table that I'm querying.

Expand|Select|Wrap|Line Numbers
  1. CREATE  TABLE IF NOT EXISTS `test`.`scalar_value_measurements` (
  2.   `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
  3.   `collect_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
  4.   `value` FLOAT(11) NOT NULL ,
  5.   `sensor_id` BIGINT(20) UNSIGNED NOT NULL ,
  6.   PRIMARY KEY (`id`, `sensor_id`) ,
  7.   UNIQUE INDEX `id` (`id` ASC) ,
  8.   INDEX `fk_scalar_value_measurements_sensor_config1` (`sensor_id` ASC) ,
  9.   INDEX `collect_time` USING BTREE (`collect_time` ASC) ,
  10.   INDEX `sensor_id` USING BTREE (`sensor_id` ASC) ,
  11.   CONSTRAINT `fk_scalar_value_measurements_sensor_config1`
  12.     FOREIGN KEY (`sensor_id` )
  13.     REFERENCES `test`.`sensor_config` (`sensor_id` )
  14.     ON DELETE NO ACTION
  15.     ON UPDATE NO ACTION)
  16. ENGINE = InnoDB
  17. AUTO_INCREMENT = 5985843
  18. DEFAULT CHARACTER SET = utf8
  19. -- COLLATE = latin1_swedish_ci;
  20. COLLATE = utf8_general_ci;
Dec 3 '12 #1
1 2208
Rabbit
12,516 Expert Mod 8TB
Unless your table has a ton of records that you don't need to return, I don't see any reason for your query to be slow. It's a fairly simple query.

MySql uses the primary key as the clustered index. To create your clustered index, add the fields you want included in your cluster to the primary key. You can also try a secondary index first so that you don't have to modify the primary key.
Dec 3 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: LaidBackWebSage | last post by:
Hi, All! I've got a very large table (149 fields -- I know, I didn't create it, and I'm working on changing it...) and I need to add an index to speed up a join query. However, when I run the...
3
by: Jim | last post by:
I put a clustered index on a table with 2 columns -nationalityid int autoincrement PK -nationality varchar(50) the clustered index is on nationality however; when i do a select i still...
3
by: Jack A | last post by:
OK Guys. I'm fed up of the query below taking too much time. I CANT change the query since it is generated by a 3rd party product. I can change indexes and add new indexes though. The schema of...
4
by: Tryfon Gavriel | last post by:
Hi all I recently noticed when trying to optimise a major query of a chess website I am the webmaster of, that adding an order by for "gamenumber" which is a clustered index field as in for...
2
by: Miss Livvy | last post by:
Would it be OK to use varchar(5) instead of char(5) as the first field of a composite clustered index? My gut tells me that varchar would be a bad idea, but I am not finding much information on...
5
by: jim_geissman | last post by:
One table I manage has a clustered index, and it includes some varchar columns. When it is initially created, all the columns in the clustered index are populated, and then some of the longer...
1
by: anonieko | last post by:
A lot of detailed discussion explains the difference between clustered and non-clustered indexes. But very few 'clarifies' why the term used is 'clustered'. Well, once and for all, this is my...
1
by: kashasrinath | last post by:
can we create clustered index on non primary key
2
by: ppuniversal | last post by:
Hi, I have a table in SQL Server 2000 with clustered index set on the Primary key. The primary key is a set of 5 fields(attributes). Now I want to add 1 more field into this clustered index as...
4
by: codefragment | last post by:
Hi I thought that given a table with an index, primary key and clustered index any non clustered index look ups would go via the clustered index and the primary key is irrelevant? (sql server...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.