# Optimisation of SELECT on a very large table

**URL:** <https://forums.percona.com/t/optimisation-of-select-on-a-very-large-table/223>\
**Category:** Other MySQL® Questions\
**Created:** [March 1, 2007, 5:56am UTC](https://forums.percona.com/t/optimisation-of-select-on-a-very-large-table/223 "2007-03-01T05:56:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FatalFlaw](https://avatars.discourse-cdn.com/v4/letter/f/e99b99/32.png) [@FatalFlaw](https://forums.percona.com/u/FatalFlaw)\
**Post date:** [March 1, 2007, 5:56am UTC](https://forums.percona.com/t/optimisation-of-select-on-a-very-large-table/223/1 "2007-03-01T05:56:01Z")

</div>

Hi Everyone

I have a large table (40m + records):

CREATE TABLE `Stats_Web_Access_Raw` (  
`Time` timestamp NOT NULL default CURRENT\_TIMESTAMP on update CURRENT\_TIMESTAMP,  
`Web_Group_ID` smallint(5) unsigned default NULL,  
`Web_User_ID` mediumint(cool: unsigned default NULL,  
`Secure` enum(‘Y’,‘N’) NOT NULL default ‘N’,  
`URL` varchar(200) default NULL,  
`Category` int(11) default NULL,  
`filelocation` int(11) default ‘0’,  
KEY `Time` (`Time`,`Web_Group_ID`,`Web_User_ID`),  
KEY `Time_2` (`Time`,`Web_User_ID`)  
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

The queries I need to run have the following qualities:

1. order will always be by Time or Time DESC.
2. Groups (which are identified by Web\_Group\_ID) contain users (identified by Web\_User\_ID).
3. I need to be able to query 1 or more Web\_Group\_IDs or Web\_User\_IDs, eg: ‘Web\_Group\_ID in (1,32,97,101)’

My problem is that the Time part of the index is the only part ever to be used. Here is a typical explain:

mysql\> explain select \* from Stats\_Web\_Access\_Raw where Time \> “07-02-20 10:48:57” AND Web\_User\_ID = 37 order by Time desc limit 10;  
±—±------------±---------------------±------±-------- ------±-------±--------±-----±--------±------------+  
| id | select\_type | table | type | possible\_keys | key | key\_len | ref | rows | Extra |  
±—±------------±---------------------±------±-------- ------±-------±--------±-----±--------±------------+  
| 1 | SIMPLE | Stats\_Web\_Access\_Raw | range | Time,Time\_2 | Time\_2 | 4 | NULL | 6019062 | Using where |  
±—±------------±---------------------±------±-------- ------±-------±--------±-----±--------±------------+

Note the ‘key\_len=4’ value, indicating that the Time column is the only index part to be used. The result is a slow (several 10s of seconds) query, if the first 10 records are not well distributed (ie all at one end of the table!). I tried playing with a range of different values for the max\_seeks\_for\_key value but it made no difference.

How do I get mysql to use the additional fields in the index?

Any comments/tips/suggestions very gratefully received.  
Jim

---

<div class="post-metadata">

**Author:** ![Peter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/peter/32/2_2.png) [@Peter](https://forums.percona.com/u/Peter)\
**Post date:** [March 1, 2007, 6:04am UTC](https://forums.percona.com/t/optimisation-of-select-on-a-very-large-table/223/2 "2007-03-01T06:04:25Z")

</div>

You need Time to go after WebUserId so both columns are used

If you have column with “\<” etc, all following columns in index can’t be used.

---

<div class="post-metadata">

**Author:** ![FatalFlaw](https://avatars.discourse-cdn.com/v4/letter/f/e99b99/32.png) [@FatalFlaw](https://forums.percona.com/u/FatalFlaw)\
**Post date:** [March 1, 2007, 6:20am UTC](https://forums.percona.com/t/optimisation-of-select-on-a-very-large-table/223/3 "2007-03-01T06:20:04Z")

</div>

Hi Peter,

thanks very much for getting back to me so quickly.

Peter said: “If you have column with “\<” etc, all following columns in index can’t be used.”

Is there anywhere in the docs where these rules are explained fully? I don’t recall seeing anothing covering this particular issue before and I thought I had read the docs pretty thoroughly. If not, what operators are ok? =, \<=\>, \<\>? Does changing the engine change these rules?

Ok so I have already tried reversing the indexes as you suggest. They work brilliantly where we have 1 group or user in the query. As soon as we query on several (eg ‘Web\_Group\_ID in (1,2,3,4)’) the DB engine does a filesort to get the ordering correct. If there are a large number of records reterned, the query can take minutes/hours to complete.

Is there any way to harness the fact that InnoDB clusters the primary key? I believe in my case is a monotonic 6-byte ID allocated by InnoDB (as I don’t specify I primary key in the create table), which essentially delivers and ordering by date, and could mean I could lose the Time field from the index …

Otherwise I don’t see how I can get this working any better, unless there is a solution using multiple table merging, say 1 table per web\_group\_id … ?

Thanks again  
Jim
