473,396 Members | 2,011 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,396 software developers and data experts.

Strange SELECT speed problem using mysql 4.0.xx

Hi,

i have a table with INT columns id,key,b1,b2,c1,c2, having 1.500.000
rows. 'key' and 'id' are indexed (Kardinality 385381) and id
(Kardinality 1541525).

Performing a

SELECT * FROM table WHERE key IN (10,11,12) OR key BETWEEN 20000 AND
28000 AND b1<4500000 AND b2>3954545 AND c1<4543554 AND c2>4400000
ORDER BY id LIMIT 0,100000

on that table (linux) results in ca. 100 lines within 70..80 ms (like
expected), but sometimes it takes 10 sec(!) and more although an
EXPLAIN SELECT tells me that the index of 'key' will be used:

table type possible_keys key key_len ref rows Extra
table range key key 5 NULL 11393 Using where;
Using filesort

Doing the SELECT with changing one of the IN()-values results
afterwards is quite fast SELECT's, seems that the caching of the
underlying file system is responsible for the faster speed.
2 Questions:

1. Why are there so big time differencies SELECT's like above
_although_ the 'key' index is used ?

2. How can one optimize mysql SELECTS for the table above? Are there
optimizations in mysql´s ini file for large tables (small tables dont
show such speed diefferencies) ?

thank you. Sven
Jul 20 '05 #1
3 2046
Sven Reifegerste wrote:
1. Why are there so big time differencies SELECT's like above
_although_ the 'key' index is used ?
I'm assuming the key is not unique value and quite many rows match given
key values. If there are many rows, caching the results will give speed
differences when doing queries.
2. How can one optimize mysql SELECTS for the table above? Are there
optimizations in mysql´s ini file for large tables (small tables dont
show such speed diefferencies) ?


You would need more indexes in your columns to help the key-index to
find the correct rows. But this is all only guessing, since I don't know
how many rows would this query return:
select count(*) from table WHERE key IN (10,11,12);

The main idea how ever is to try to minimize the amount of rows that
non-indexed column keys need to be compared with.

Adding indexes will how ever increase the disk space loss and slow the
insert and propably update queries also by small amount. (Only important
if you need to insert thousands of rows fast into the database.)
Jul 20 '05 #2
> > 1. Why are there so big time differencies SELECT's like above
_although_ the 'key' index is used ?
I'm assuming the key is not unique value and quite many rows match given
key values.


Right, the key is not unique. The result when using the key only (IN
and BETWEEN clauses) is in this example about 20 times bigger
(30000-40000 rows), and _normally_ it takes about 100-200ms, but
sometimes magnitudes more (10..20 seconds).
The main idea how ever is to try to minimize the amount of rows that
non-indexed column keys need to be compared with.


Sounds good. But even 30000*4 comparisons cant last 10..20 seconds??
While experimenting, I got an idea what the reason for those slow
SELECTS could be: The idea of the one-dimensional 'key' column is that
it encodes relevant information of b1,b2,c1,c2 into only one number.
So, only the 'key' column has to be used for a rough elimination of
non-potential rows (which saves alot of time for searching in 99% of
all cases), followed by fine-filtering using b1,b2,c1,c2. I realized
that SELECTs without using the 'key' longs 10..20 ms. This is the time
duration that the SELECT in question took about.

Thus, it seems that mysql decides sometimes for _not_making_use_ of
the index to filter relevant rows _although_ it would make sense. Thus
I'm really suspicious about the result of an EXPLAIN SELECT telling me
that the 'key' index is used. And if it would be used, then surely in
a non-senseful order. Its also quite strange that I cannot reproduce
any of these long-lasting SELECTs, neither by modifying some
parameters to circumvent the query cache nor by restarting mysql and
flushing tables.

Any ideas how to force mysql to use the key column?
Thank your for previous and upcoming responses!
Sven
Jul 20 '05 #3
Sven Reifegerste wrote:
Right, the key is not unique. The result when using the key only (IN
and BETWEEN clauses) is in this example about 20 times bigger
(30000-40000 rows), and _normally_ it takes about 100-200ms, but
sometimes magnitudes more (10..20 seconds).
That sounds really strange. How did you create the index column? I've
always used this syntax:

create index indexname on tablename( columnname [,2nd_columnname,...] );

because I once noticed that when I had set a column to be a key or
something, it didn't create an index for it, or if it did, it wasn't
really fast. After creating the index that way, queries worked faster so
I started using that without bothering to investigate it any further.
Sounds good. But even 30000*4 comparisons cant last 10..20 seconds??
I agree, it shouldn't, so it is either checking all or some of the
columns that it shouldn't.
Any ideas how to force mysql to use the key column?


SELECT * FROM t1, t2 FORCE INDEX (index_for_column)
WHERE t1.col_name=t2.col_name;

http://dev.mysql.com/doc/mysql/en/Optimizer_Issues.html
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Google Mike | last post by:
I have RH9 and am using the PHP and MySQL that came with it. I was doing fine with all manner of my web pages for this app until I started having this very strange problem. It's a work order...
4
by: Bruce A. Julseth | last post by:
My MySQL.user table (user, host, password) looks like the following: +---------+-----------+---------------------+ | user | host | password |...
16
by: lkrubner | last post by:
Are there any benchmarks on how much an extra, unneeded VARCHAR, CHAR, INT, BIGINT, TEXT or MEDIUMTEXT slows down a database call with MySql? PostGre info would also be useful. I'm trying to...
0
by: Neculai Macarie | last post by:
Hi! Using Union and Order By gives strange behaviour in the following test-case: drop table if exists gallery; drop table if exists gallery_categ; # create test tables create table gallery...
0
by: Nedelcho Stanev | last post by:
Hello All, I have strange problem with libodbc++ ( 0.2.3 or 0.2.2 ). i'm using mysql-4.0.14 , MyODBC-3.51.06 and unixODBC-2.2.6 configured with following options 1.MySQL ../configure...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
3
by: Jay | last post by:
I have a table with a varchar(255) column, it has 111,000 rows. When I do a select distinct on that column it takes 16 seconds and returns about 25 distinct values, I'd like it to take much less...
3
by: Vague | last post by:
Hello There, I've recently been working on a large project, very database driven, in C# on windows systems, calling a MySQL database on a linux box. The project is going very well. Except that...
9
by: Dave | last post by:
Hi guys, I have just set up a duplicate server running: apache 2.54, mysql 5.04 and php 5.04 This is the same setup as as the server we are using now, apart from the hardware inside. I have...
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:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.