473,473 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Improve SELECT command

Dear MySQL-ians,

I perform a SELECT on my database, but it takes over a minute for every
run. I have to run it over 10000 times (with different values in the
WHERE), so it takes way too long. A was therefore wondering if I could
improve the query speed. Below you find the query. It is based on the
ratio between a pixel (pix) vs. the average of its neighbourhood (from
geo) in the same table (vgt) based on additional requirements
(mgba,mgsc,eco).

Any comment on the query/design is welcome.

SELECT vgt.obs,pix.NDVI/AVG(vgt.NDVI)
FROM vgtData.VGT as pix,
(vgt.meta_gsc_2000 as mgsc RIGHT JOIN vgt.gsc2000 as gsc ON
gsc.gsc2000_XX = mgsc.id)
INNER JOIN vgt.geo_1000 as geo ON geo.id = gsc.id
INNER JOIN vgtData.VGT as vgt ON geo.id = vgt.id
INNER JOIN vgt.v_ecoclim AS eco ON geo.id = eco.id
INNER JOIN (vgt.gba2000 as gba LEFT JOIN vgt.meta_gba_2000 as mgba ON
gba.gba2000_XX = mgba.id) ON gba.id=geo.id
WHERE pix.obs = vgt.obs
AND pix.id = 200265
AND vgt.obs > 55
AND vgt.obs <= 127
AND (geo.X_coord BETWEEN 1525 AND 1545)
AND (geo.Y_coord BETWEEN 110 AND 130)
AND mgba.burnt=0
AND mgsc.burnt=0
AND eco.v_landcov=3
AND eco.v_lowreb=10
AND vgt.B<27
AND vgt.SWIR<250
AND pix.B<27
AND pix.SWIR<250
GROUP BY vgt.obs;

The EXPLAIN command gives me the following result:
table type possible_keys key len ref
rows Extra
----- ------ -------------------------- -------- -- --------------
----- --------------------------------------------
eco ref PRIMARY,v_landcov,v_lowreb v_lowreb 3 const
17021 Using where; Using temporary; Using filesort
geo eq_ref PRIMARY,X_coord,Y_coord PRIMARY 3 eco.id 1
Using where
gsc eq_ref PRIMARY,gsc2000_XX PRIMARY 3 geo.id 1

mgsc eq_ref PRIMARY,burnt PRIMARY 3 gsc.gsc2000_XX 1
Using where
vgt ref id,obs id 4 geo.id 1
Using where
pix ref id,obs id 4 const 156
Using where
gba eq_ref PRIMARY,gba2000_XX PRIMARY 3 geo.id 1

mgba eq_ref PRIMARY,burnt PRIMARY 3 gba.gba2000_XX 1
Using where
and the tables look like;
################################################## #
vgtData.VGT
Field Type Null Key Default Extra
----- -------------------- ---- --- ------- ------
id mediumint(8) unsigned YES MUL (null)
obs tinyint(4) unsigned YES MUL (null)
B tinyint(4) unsigned YES (null)
SWIR tinyint(4) unsigned YES (null)
NDVI tinyint(4) unsigned YES (null)
################################################## ##

################################################## #######
vgt.geo_1000
Field Type Null Key Default Extra
------ --------------------- ---- --- ------- ---------
id mediumint(8) unsigned PRI (null) auto_incr
X_coord smallint(6) unsigned YES MUL (null)
Y_coord smallint(7) unsigned YES MUL (null)
################################################## #######
################################################## #######
vgt.v_ecoclim
Field Type Null Key Default Extra
--------- --------------------- ---- --- ------- ---------
id mediumint(8) unsigned PRI (null) auto_incr
v_lowreb smallint(7) YES MUL (null)
v_landcov smallint(7) YES MUL (null)
################################################## #######

################################################## #######
vgt.gba2000
Field Type Null Key Default Extra
---------- --------------------- ---- --- ------- ---------
id mediumint(8) unsigned PRI (null) auto_incr
gba2000_XX smallint(7) unsigned YES MUL (null)
################################################## #######

################################################## #######
vgt.meta_gba_2000
Field Type Null Key Default Extra
----- --------------------- ---- ---- ------- --------
id mediumint(8) unsigned PRI (null) auto_incr
burnt tinyint(1) YES MUL (null)
################################################## #######

################################################## #######
vgt.gsc2000
Field Type Null Key Default Extra
---------- --------------------- ---- --- ------- ---------
id mediumint(8) unsigned PRI (null) auto_incr
gsc2000_XX smallint(7) unsigned YES MUL (null)
################################################## #######

################################################## #######
vgt.meta_gsc_2000
Field Type Null Key Default Extra
----- --------------------- ---- ---- ------- --------
id mediumint(8) unsigned PRI (null) auto_incr
burnt tinyint(1) YES MUL (null)
################################################## #######

I am introducing indexes on vgtData.SWIR and vgtData.B but they have a
very low cardinality, so I assume it won't make a big difference.

Does anyone has a suggestion to make it faster?

Thanx in advance and kind regards,
Stef

May 12 '06 #1
3 2047
st***************@agr.kuleuven.ac.be wrote:
SELECT vgt.obs,pix.NDVI/AVG(vgt.NDVI) .... GROUP BY vgt.obs;
This is a bit odd, because you are not grouping by pix.NDVI. In some
RDBMS implementations, it is mandatory to group by all columns mentioned
in the select-list that are not used inside aggregate functions. MySQL
permits this. However, the value it returns for pix.NDVI will be some
arbitrarily chosen value in the group. Though in this case I assume you
restrict the pix table sufficiently, so it doesn't cause any ambiguity.
The EXPLAIN command gives me the following result:
table type possible_keys key len ref
rows Extra
----- ------ -------------------------- -------- -- --------------
----- --------------------------------------------
eco ref PRIMARY,v_landcov,v_lowreb v_lowreb 3 const
17021 Using where; Using temporary; Using filesort
geo eq_ref PRIMARY,X_coord,Y_coord PRIMARY 3 eco.id 1
Using where
gsc eq_ref PRIMARY,gsc2000_XX PRIMARY 3 geo.id 1

mgsc eq_ref PRIMARY,burnt PRIMARY 3 gsc.gsc2000_XX 1
Using where
vgt ref id,obs id 4 geo.id 1
Using where
pix ref id,obs id 4 const 156
Using where
gba eq_ref PRIMARY,gba2000_XX PRIMARY 3 geo.id 1

mgba eq_ref PRIMARY,burnt PRIMARY 3 gba.gba2000_XX 1
Using where
The temporary table & filesort mentioned in the line for eco is probably
the culprit. These are on-disk data operations, which is very slow
compared to in-memory operations. The GROUP BY is probably requiring
the temp table, because it isn't using the index on vgt.obs. You could
try using "FORCE INDEX (obs)" to make it use that index. But I'm not
sure that this would relieve the filesort, and it would also prevent use
of the vg.id index.
Does anyone has a suggestion to make it faster?


MySQL 5 _might_ deal with this better. In some circumstances, MySQL 5
can use more than one index per table. MySQL 4.1 and earlier have a
limitation of one index per table in a given query. MySQL 5 also has
some improvements to the filesort algorithm.

You could try increasing your key_buffer_size server parameter, so that
it's large enough to contain all the indexes used (according to the
EXPLAIN report). Also, preload the indexes.

See
http://dev.mysql.com/doc/refman/5.0/...variables.html
http://dev.mysql.com/doc/refman/5.0/...reloading.html

Regards,
Bill K.
May 12 '06 #2
st***************@agr.kuleuven.ac.be wrote:
Dear MySQL-ians,

I perform a SELECT on my database, but it takes over a minute for every
run. I have to run it over 10000 times (with different values in the
WHERE), so it takes way too long. A was therefore wondering if I could .... SELECT vgt.obs,pix.NDVI/AVG(vgt.NDVI)
FROM vgtData.VGT as pix,
(vgt.meta_gsc_2000 as mgsc RIGHT JOIN vgt.gsc2000 as gsc ON
gsc.gsc2000_XX = mgsc.id)

....
Perhaps you can store the right join in a temporary table
and reuse it for the subsequent queries.

May 14 '06 #3
Thanks Bill and Bernard.

I am indeed upgrading my mysql version to 5.0 and hoping to use the
improved selection criteria there.

Thanks for your help!
Stef

May 18 '06 #4

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

Similar topics

0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
3
by: Leader | last post by:
Hi All, I am getting slower performance of select statements in MS SQL. I am finding select statements in MS SQL are even slower than MS ACCESS. Is there any way to improve the performance of...
2
by: Jaidev Paruchuri | last post by:
I have a table called work_order which has over 1 million records and a contractor table which has over 3000 records. When i run this query ,it takes long time since its grouping by contractor...
6
by: yongsing | last post by:
I have a table containing 5.1 million rows. The DDL of the table is shown below (only relevant parts shown). CREATE TABLE XXX.XXXX ( ... SERIAL CHAR(12) NOT NULL, ENDDATE DATE NOT...
13
by: MLH | last post by:
I have a RDBMS app consisting of 3 primary mdb's... 1) a front-end with a few STATIC tables and the other menagerie of objects 2) a back-end with most of my DYNAMIC tables. I'll call it my main...
8
by: Michael C | last post by:
Anyone have any hints on improving the performance of C# UI? I'm filling a TreeView and ListView with information returned by a SQLDataReader and information read from the Registry. I'm working...
6
by: Jéjé | last post by:
Hi, hoew can I improve the compilation process of a sharepoint website? my server is: 2 * P3 Xeom 1ghz 4go ram 2 * 36gb (mirror for OS and website) 2 * 36 Raid 0 (stripping; for temp files...
5
by: Martin Bischoff | last post by:
Hi, is it possible to modify the values of a SqlDataSource's select parameters in the code behind before the select command is executed? Example: I have an SqlDataSource with a...
2
by: lelandhuang | last post by:
I am developing reporting service and using lots of 'LEFT OUTER JOIN', I am worried about the performance and want to use some subquery to improve the performance. Could I do that like below, ...
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.