By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,480 Members | 763 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,480 IT Pros & Developers. It's quick & easy.

Speeding up select queries on table with 3million rows.

P: n/a
Hi,
I'm new to databases :) I need help speeding up select queries on my
data which are currently taking 4-5 seconds. I set up a single large
table of coordinates data with an index on the fields I use most
frequently in select queries. The data is about 100MB and index is
80MB. The table has the following structure:

CREATE TABLE `ptimes` (
`id` INT UNSIGNED NOT NULL ,
`rc` CHAR ( 1 ) UNSIGNED NOT NULL ,
`lat` INT NOT NULL ,
`long` INT NOT NULL ,
`ccode` CHAR( 2 ) NOT NULL ,
`admcode` CHAR( 4 ) NOT NULL ,
`nt` CHAR( 1 ) NOT NULL ,
`name` VARCHAR( 30 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
INDEX ( `lat` , `long` , `ccode` , `admcode` , `name` )
);

The rc, nt and ccode fields' data are repeated a lot. I mostly need to
run queries to lookup lat and long fields, given rc, ccode, admcode and
name. I won't be doing many insert or updates. Disk space is not a
problem. System is RedHat Linux AS3 on Pentium 4 with 512MB.

I appreciate any help improving the table structure. Would it be
possible to speed up the queries to under .5 sec on this
hardware/software?

Thanks!

Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
mf*****@gmail.com wrote:
The rc, nt and ccode fields' data are repeated a lot. I mostly need to
run queries to lookup lat and long fields, given rc, ccode, admcode and
name.


I think you should create individual indexes for the fields you use for
search criteria. That is, rc, ccode, admcode and name each should be
indexed individually (not a compound index as you have defined above).

In my opinion, compound indexes are useful for enforcing uniqueness
across a set of columns. But in practice for speeding up queries, they
frequently do not help, unless your search criteria use a left-sided set
of the column. That is, if you were searching with criteria of lat,
long, and ccode, the index could be used. But if you search with
criteria in ccode, admcode, and name, then the index cannot be used.

To understand why, think of a telephone book. A telephone book is
indexed by the equivalent of a compound index on (lastname, firstname).
If I do a search for all people named "John", the indexed order of the
book is useless to me; I still have to do a page-by-page scan of the book.

And finally, it appears in your table definition that the rc field is
not indexed at all.

So I advise this change:

ALTER TABLE `ptimes` ADD INDEX (`rc`);
ALTER TABLE `ptimes` ADD INDEX (`ccode`);
ALTER TABLE `ptimes` ADD INDEX (`admcode`);
ALTER TABLE `ptimes` ADD INDEX (`name`);

Then try your query again. MySQL might still decide not to use indexes
for columns with a small number of distinct values. I bet the most
useful index you could create would be one on the `name` field.

Regards,
Bill K.
Jul 23 '05 #2

P: n/a
<mf*****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
<snip>
CREATE TABLE `ptimes` (
`id` INT UNSIGNED NOT NULL ,
`rc` CHAR ( 1 ) UNSIGNED NOT NULL ,
`lat` INT NOT NULL ,
`long` INT NOT NULL ,
`ccode` CHAR( 2 ) NOT NULL ,
`admcode` CHAR( 4 ) NOT NULL ,
`nt` CHAR( 1 ) NOT NULL ,
`name` VARCHAR( 30 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
INDEX ( `lat` , `long` , `ccode` , `admcode` , `name` )
); <snip> I appreciate any help improving the table structure. Would it be
possible to speed up the queries to under .5 sec on this
hardware/software?


Change [name VARCHAR(30)] to [name CHAR(30)].

By allowing the table to assign a fixed length (30) characters, it will cost
you a little bit of disk space BUT it will pay back by allowing MySQL to
declare the table STATIC rather than DYNAMIC and make indexing and locating
records more efficient. Dramatically more efficient for large tables.

There is no downside to this other than it will consume more diskspace - and
probably not much more at that.
Thomas Bartkus
Jul 23 '05 #3

P: n/a
Thomas Bartkus wrote:
<mf*****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
<snip>
CREATE TABLE `ptimes` (
`id` INT UNSIGNED NOT NULL ,
`rc` CHAR ( 1 ) UNSIGNED NOT NULL ,
`lat` INT NOT NULL ,
`long` INT NOT NULL ,
`ccode` CHAR( 2 ) NOT NULL ,
`admcode` CHAR( 4 ) NOT NULL ,
`nt` CHAR( 1 ) NOT NULL ,
`name` VARCHAR( 30 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
INDEX ( `lat` , `long` , `ccode` , `admcode` , `name` )
);

<snip>
I appreciate any help improving the table structure. Would it be
possible to speed up the queries to under .5 sec on this
hardware/software?


Change [name VARCHAR(30)] to [name CHAR(30)].

By allowing the table to assign a fixed length (30) characters, it
will cost
you a little bit of disk space BUT it will pay back by allowing MySQL
to
declare the table STATIC rather than DYNAMIC and make indexing and
locating records more efficient. Dramatically more efficient for large
tables.

There is no downside to this other than it will consume more diskspace
- and probably not much more at that.


Note also http://dev.mysql.com/doc/mysql/en/si...n-changes.html

Although the `admcode` column is declared as a char it will actually be
a varchar under MySQL's silent column changes. As long as there are
*no* varchars in the table then they can all be fixed length char
types. Just another of those MySQL "gotchas"

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 23 '05 #4

P: n/a

"Chris Hope" <bl*******@electrictoolbox.com> wrote in message
news:11*************@216.128.74.129...
Thomas Bartkus wrote:
<mf*****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
<snip>
CREATE TABLE `ptimes` (
`id` INT UNSIGNED NOT NULL ,
`rc` CHAR ( 1 ) UNSIGNED NOT NULL ,
`lat` INT NOT NULL ,
`long` INT NOT NULL ,
`ccode` CHAR( 2 ) NOT NULL ,
`admcode` CHAR( 4 ) NOT NULL ,
`nt` CHAR( 1 ) NOT NULL ,
`name` VARCHAR( 30 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
INDEX ( `lat` , `long` , `ccode` , `admcode` , `name` )
); <snip>
I appreciate any help improving the table structure. Would it be
possible to speed up the queries to under .5 sec on this
hardware/software?


Change [name VARCHAR(30)] to [name CHAR(30)].

<snip>
Note also http://dev.mysql.com/doc/mysql/en/si...n-changes.html

Although the `admcode` column is declared as a char it will actually be
a varchar under MySQL's silent column changes. As long as there are
*no* varchars in the table then they can all be fixed length char
types. Just another of those MySQL "gotchas"

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/


His "name" field is the only declared VARCHAR. It is the only non-fixed
length field he has so I *think* he can ALTER it to CHAR and be done with
it. You are correct to point out "As long as there are *no* varchars in the
table " and this is apparently the case here - or at least it will be when
he changes that one column.

I have noted in the past that if there are multiple VARCHAR fields that
MySQL will refuse (without telling you!) to change it to CHAR. How rude!
The only way I have found out of this is to re-create the whole table in one
swoop with all fixed width CHAR in a CREATE TABLE declaration. However,
with only one single VARCHAR, I think you can just ALTER it to CHAR.

But - I have been known to be wrong on rare occasion :-)
Thomas Bartkus
Jul 23 '05 #5

P: n/a
Thank you for the suggestions. The queries are taking around 2 secs
after I created separate indexes and changed varchars to chars. How
does google do it in milliseconds?? Is it just super hardware?

What happens when there are both multicolumn and single column indexes?
Which one does MySQL use?

I'd appreciate if anyone could suggest a good introductory book on
these database topics. I find this fascinating.

Yahya

Jul 23 '05 #6

P: n/a
mf*****@gmail.com wrote:
Thank you for the suggestions. The queries are taking around 2 secs
after I created separate indexes and changed varchars to chars. How
does google do it in milliseconds?? Is it just super hardware?
They don't use a relational database; IIRC it's some sort of file based
tree system.
What happens when there are both multicolumn and single column
indexes? Which one does MySQL use?
If you index col1, col2 and col3 in a multicolumn index and nothing
else, then a query on col2 or col3 alone won't be able to use the index
and you would need to create separate indexes for them.

However, if you query col1 only it will use this index; if you use col1
and col2 it will use this index; same for querying all three columns.

If you want to see what MySQL is doing when it runs a query use EXPLAIN
eg "explain select foo from bar where ..."
I'd appreciate if anyone could suggest a good introductory book on
these database topics. I find this fascinating.


--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 23 '05 #7

P: n/a
mf*****@gmail.com wrote:
Thank you for the suggestions. The queries are taking around 2 secs
after I created separate indexes and changed varchars to chars. How
does google do it in milliseconds?? Is it just super hardware?
Super hardware (actually a whole building of hardware) and some patented
proprietary indexing technology that is quite unlike a mere RDBMS.

I'd say if you need performance and capabilities like Google, MySQL is
not the right technology to use. You might want to consider one of
their rackmount servers:

http://www.google.com/enterprise/
What happens when there are both multicolumn and single column indexes?
Which one does MySQL use?


MySQL has a series of criteria for when to use indexes. You should read
chapter 7 of the MySQL online docs for starters:

http://dev.mysql.com/doc/mysql/en/my...imization.html

The MySQL web site also has a few pages for books that are useful for
MySQL users. The one I see that you might find most interesting is "SQL
Performance Tuning" by Peter Gulutzan, Trudy Pelzer.

Regards,
Bill K.
Jul 23 '05 #8

P: n/a
mf*****@gmail.com wrote:
How does google do it in milliseconds?? Is it just super hardware?


Here's a page where you can read quite a bit about the Google technology!

http://en.wikipedia.org/wiki/Google

Regards,
Bill K.
Jul 23 '05 #9

P: n/a
On 1 Feb 2005 13:20:31 -0800, mf*****@gmail.com wrote:
Thank you for the suggestions. The queries are taking around 2 secs
after I created separate indexes and changed varchars to chars. How
does google do it in milliseconds?? Is it just super hardware?

What happens when there are both multicolumn and single column indexes?
Which one does MySQL use?

I'd appreciate if anyone could suggest a good introductory book on
these database topics. I find this fascinating.

Yahya


"MySQL" by Paul DuBois covers a lot of these topics pretty well.

Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.