473,382 Members | 1,646 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,382 software developers and data experts.

trying to optimize table and query

110 100+
I'm fairly new to sql, and am having a problem with a query taking forever to run.
it's 8 seconds to run each query.

I think I have my indexes correct, but I can't seem to find the output which describes the indexes.

I have done an 'explain' on the query, and this is what I got
Expand|Select|Wrap|Line Numbers
  1.  id | select_type | table   | type | possible_keys          | key            | key_len | ref                      | rows   | Extra                           |
  2. +----+-------------+---------+------+------------------------+----------------+---------+--------------------------+--------+---------------------------------+
  3. |  1 | SIMPLE      | artists | ALL  | link_bandid_idx        | NULL           | NULL    | NULL                     | 920243 | Using temporary; Using filesort |
  4. |  1 | SIMPLE      | shows   | ref  | lat_idx,get_bandid_idx | get_bandid_idx | 4       | hear.artists.bandid |      7 | Using where          
  5.  
does this look REALLY bad? the 9202043 is worrysome, but I have an UNIQUE index on the bandid field in the artists table. bandid i not unique in shows.

any idea on how i can improve the performance on this?
May 21 '08 #1
5 1346
code green
1,726 Expert 1GB
Well you could start by showing us the query....
May 21 '08 #2
pedalpete
110 100+
Sorry about that code green.
I can understand how that would help :) it was late, and I had been looking at this for WAY too long.

the query is

Expand|Select|Wrap|Line Numbers
  1. SELECT shows.bandid, shows.lat, shows.long, shows.address, shows.title, shows.city, shows.state, shows.zip, shows.date, shows.time, shows.accuracy, artists.name, artists.img, artists.genre, artists.genre FROM shows JOIN artists ON shows.bandid=artists.bandid WHERE `long` > '-98.70172684058' AND `long` < '-97.97708915942' AND `lat` > '29.14524415942' AND `lat` < '29.86988184058' AND shows.date > '2008-05-21' ORDER BY date LIMIT 0,30
  2.  
yesterday I had tried removing the limit, and then order, stripping things down, but wasn't noticing a huge difference.

I just tried it again without the ORDER BY, and leaving the limit, and that quickens it up considerably. Unfortunately the order is incredibly important. I could try to do the order client-side, but that would mean i would need to get the entire list back, and it seems the LIMIT speeds up the results considerably.

The time it is taking to complete the queries is as follows (though I don't exactly understand why there are two figures.
1) query with ORDER BY and LIMIT - 30 rows fetched in 0.0151(8.8205s)
2) query without ORDER BY with LIMIT - 30 rows fetched in 0.0175s(0.3888s)
3) query without ORDER BY without LIMIT - 1671 rows fetched in 8.1671s(0.3986s)
May 21 '08 #3
code green
1,726 Expert 1GB
It is a fairly straightforward query.
It may be more a case of optimising the tables rather than the query:
INDEXes, VARCHAR fields, segmenting.
Expand|Select|Wrap|Line Numbers
  1. SELECT shows.bandid, shows.lat, shows.long, shows.address,
  2. shows.title, shows.city, shows.state, shows.zip, shows.date, shows.time,
  3. shows.accuracy, artists.name, artists.img, artists.genre, artists.genre 
  4. FROM shows JOIN artists 
  5. ON shows.bandid = artists.bandid 
  6. WHERE `long` > '-98.70172684058' 
  7. AND `long` < '-97.97708915942' 
  8. AND `lat` > '29.14524415942' 
  9. AND `lat` < '29.86988184058' 
  10. AND shows.date > '2008-05-21' 
  11. ORDER BY date LIMIT 0,30
OK. If the fields are numeric then remove the quotation marks, they are not needed and will slow down.
If the same results are obtained with LEFT JOIN use that.
Not much but worth a try.
Also I sometimes experiment by moving the WHERE conditions into the ON clause
Expand|Select|Wrap|Line Numbers
  1. ON (shows.bandid = artists.bandid 
  2. AND `long` > -98.70172684058 
  3. AND `long` < -97.97708915942 
  4. AND `lat` > 29.14524415942 
  5. AND `lat` < 29.86988184058 
  6. AND shows.date > '2008-05-21')
May 21 '08 #4
pedalpete
110 100+
Thanks Code Green,

I suspected that it was the indexes which was causing the problem, which is why I started out with the 'EXPLAIN' response in my first post.
I thought from that, it should point out if I have the right indexes in place. Maybe I'm wrong.

I also just realized that the bandid field in artists is VARCHAR instead of INT, that would make a BIG difference...
Now my response times are 0.03 seconds!!!

I REALLY appreciate your help.
pete
May 21 '08 #5
code green
1,726 Expert 1GB
Well done.
Strings are notoriously slow to check.
Go for INT wherever possible.
Don't forget to remove the quotes
May 22 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Andreas Falck | last post by:
Hi, I ran the code below on two different versions, 4.0.14 and 4.0.16 respectively, both running RH 7.3 on intel. In version mysql server version 4.0.14 the SELECT privelege suffices for...
6
by: Bruce D | last post by:
Could someone please help to explain why the following query isn't using the index... explain select id from kbm where state = 'MA' table type possible_keys key key_len ref rows Extra...
7
by: janet | last post by:
HI, We had some data which had low cardinality (86) , the count for that table is 9 million rows. How to optimize query which involve joining this table on that column? I'm not sure if...
5
by: xeqister | last post by:
Greetings all, We have a complicated statement in DB2 which takes long hour to complete and we have created most of the indexes. Does anybody knows how to tune the following statement to optimize...
0
by: Michal Hlavac | last post by:
Hello, I have one main table and 2 many-to-many relation tables and 2 one-to-many relation table. Main table has 150 000 rows and many-to-many tables have about 300 000 rows. I am creating...
4
by: Huaer.XC | last post by:
>From the following MySQL command: EXPLAIN SELECT * FROM t1 JOIN t2 ON (t1.id = t2.id) JOIN t3 ON t3.name = t1.name WHERE t1.id IN(123, 124); which result is:...
19
by: octangle | last post by:
This code is attempting to find records that have a RegJrnID that does not occur more than one time in the table. The reason that I want to find records with non-duplicated RegJrnID values is to...
1
by: acornejo | last post by:
Hi All I've the following code I need to optimize. Currently tblOutgoing is about 250K registers and growing at a rate of about 20k records per day. This code takes me over 5 secs to run on each...
0
by: pedalpete | last post by:
Hey MySql Gurus, I've been having some issues with my database starting a few days ago, though I haven't changed any of the db code in at least 3 weeks. I do some daily maintenance on my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.