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
- SELECT collect_time, sensor_id, value
- FROM scalar_value_measurements
- WHERE sensor_id = 254
- AND collect_time BETWEEN '2012-12-03 07:52:57'
- AND NOW()
- ORDER BY collect_time ASC
Below is the MySQL statement used to create the table that I'm querying.
Expand|Select|Wrap|Line Numbers
- CREATE TABLE IF NOT EXISTS `test`.`scalar_value_measurements` (
- `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
- `collect_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
- `value` FLOAT(11) NOT NULL ,
- `sensor_id` BIGINT(20) UNSIGNED NOT NULL ,
- PRIMARY KEY (`id`, `sensor_id`) ,
- UNIQUE INDEX `id` (`id` ASC) ,
- INDEX `fk_scalar_value_measurements_sensor_config1` (`sensor_id` ASC) ,
- INDEX `collect_time` USING BTREE (`collect_time` ASC) ,
- INDEX `sensor_id` USING BTREE (`sensor_id` ASC) ,
- CONSTRAINT `fk_scalar_value_measurements_sensor_config1`
- FOREIGN KEY (`sensor_id` )
- REFERENCES `test`.`sensor_config` (`sensor_id` )
- ON DELETE NO ACTION
- ON UPDATE NO ACTION)
- ENGINE = InnoDB
- AUTO_INCREMENT = 5985843
- DEFAULT CHARACTER SET = utf8
- -- COLLATE = latin1_swedish_ci;
- COLLATE = utf8_general_ci;