473,399 Members | 4,192 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,399 software developers and data experts.

speeding up a REALLY slow querey?

LRW
I have a querey formed with a few joins and a lot of fields, and no
kidding, it takes up to 2 minutes for the PHP page to poulate with all
the results (usually around 300).

Now, we have a pretty good server: dual Pentium 4 CPUs and a gig of
RAM. And all other queries seem to fly.
So there must be something about this querey that must be horribly
inefficient and badly written.

Can anyone give me any tips on how I can better this?

Thanks!
Liam

$sql = "SELECT orders.companyid,orders.ordernum,orders.address AS
o_address,orders.city AS o_city,orders.state AS o_state,orders.zipcode
AS o_zipcode,orders.firstname AS o_firstname,orders.lastname AS
o_lastname,".
"tbl_ih_accounts.company,tbl_ih_accounts.addre ss AS
ih_address,tbl_ih_accounts.city AS ih_city,tbl_ih_accounts.state AS
ih_state,tbl_ih_accounts.zipcode AS
ih_zipcode,tbl_ih_accounts.firstname AS
ih_firstname,tbl_ih_accounts.lastname AS ih_lastname,".
"accounts.address AS a_address,accounts.city AS a_city,accounts.state
AS a_state,accounts.zipcode AS a_zipcode,accounts.firstname AS
a_firstname,accounts.lastname AS a_lastname, ".
"orderdate, sp_local, inhouse ".
"FROM orderstatus ".
"LEFT JOIN orders ON (orderstatus.ordernum = orders.ordernum) ".
"LEFT JOIN tbl_ih_accounts ON (tbl_ih_accounts.id = ihid) ".
"LEFT JOIN accounts ON (accounts.username = accountid) ".
"WHERE submitted <> 'yes' ORDER BY orders.ordernum ASC";
Jul 20 '05 #1
6 1441
LRW wrote:
Can anyone give me any tips on how I can better this?


Have you tried adding indexes to columns that are used in the search?
Jul 20 '05 #2

"Aggro" <sp**********@yahoo.com> wrote in message
news:_V***************@read3.inet.fi...
LRW wrote:
Can anyone give me any tips on how I can better this?


Have you tried adding indexes to columns that are used in the search?


Interesting note in the MySQL documentation which seems to say:

Even if indexes are used, they can be improved upon by making sure the
columns are all declared the same size. Especially with the older ISAM
tables --indexes may be skipped altogether unless the length of the declared
columns is equal.

Reference:
http://dev.mysql.com/doc/mysql/en/EXPLAIN.html

Depending on the table type, I think it also helps to run the analyze
command on the tables:

mysql> analyze table <tablename>;
Regards,

Mike Chirico
Jul 20 '05 #3
if the underlying tables are big, make sure to do ANALYZE on them. Made
a big difference for me.

LRW wrote:
I have a querey formed with a few joins and a lot of fields, and no
kidding, it takes up to 2 minutes for the PHP page to poulate with all
the results (usually around 300).

Now, we have a pretty good server: dual Pentium 4 CPUs and a gig of
RAM. And all other queries seem to fly.
So there must be something about this querey that must be horribly
inefficient and badly written.

Can anyone give me any tips on how I can better this?

Thanks!
Liam

$sql = "SELECT orders.companyid,orders.ordernum,orders.address AS
o_address,orders.city AS o_city,orders.state AS o_state,orders.zipcode
AS o_zipcode,orders.firstname AS o_firstname,orders.lastname AS
o_lastname,".
"tbl_ih_accounts.company,tbl_ih_accounts.addre ss AS
ih_address,tbl_ih_accounts.city AS ih_city,tbl_ih_accounts.state AS
ih_state,tbl_ih_accounts.zipcode AS
ih_zipcode,tbl_ih_accounts.firstname AS
ih_firstname,tbl_ih_accounts.lastname AS ih_lastname,".
"accounts.address AS a_address,accounts.city AS a_city,accounts.state
AS a_state,accounts.zipcode AS a_zipcode,accounts.firstname AS
a_firstname,accounts.lastname AS a_lastname, ".
"orderdate, sp_local, inhouse ".
"FROM orderstatus ".
"LEFT JOIN orders ON (orderstatus.ordernum = orders.ordernum) ".
"LEFT JOIN tbl_ih_accounts ON (tbl_ih_accounts.id = ihid) ".
"LEFT JOIN accounts ON (accounts.username = accountid) ".
"WHERE submitted <> 'yes' ORDER BY orders.ordernum ASC";

Jul 20 '05 #4
"LRW" <de**@celticbear.com> wrote in message
I have a querey formed with a few joins and a lot of fields, and no
kidding, it takes up to 2 minutes for the PHP page to poulate with all
the results (usually around 300). $sql = "SELECT orders.companyid,orders.ordernum,orders.address AS
o_address,orders.city AS o_city,orders.state AS o_state,orders.zipcode
AS o_zipcode,orders.firstname AS o_firstname,orders.lastname AS
o_lastname,".
"tbl_ih_accounts.company,tbl_ih_accounts.addre ss AS
ih_address,tbl_ih_accounts.city AS ih_city,tbl_ih_accounts.state AS
ih_state,tbl_ih_accounts.zipcode AS
ih_zipcode,tbl_ih_accounts.firstname AS
ih_firstname,tbl_ih_accounts.lastname AS ih_lastname,".
"accounts.address AS a_address,accounts.city AS a_city,accounts.state
AS a_state,accounts.zipcode AS a_zipcode,accounts.firstname AS
a_firstname,accounts.lastname AS a_lastname, ".
"orderdate, sp_local, inhouse ".
"FROM orderstatus ".
"LEFT JOIN orders ON (orderstatus.ordernum = orders.ordernum) ".
"LEFT JOIN tbl_ih_accounts ON (tbl_ih_accounts.id = ihid) ".
"LEFT JOIN accounts ON (accounts.username = accountid) ".
"WHERE submitted <> 'yes' ORDER BY orders.ordernum ASC";


Is column submitted from the orders table? The query may be slow because
very few rows meet the condition, and the system probably does a full table
scan because of all records in the table.

What are the possible values of submitted? If just true and false, or 'yes'
and 'no', or 'Y' or 'N', or 1 or 0, then try

create index specialname on orders ( submitted, ordernum );

select ...
from ...
where orders.submitted = 'no'
order by orders.submitted, orders.ordernum;
Jul 20 '05 #5
LRW
"Mike Chirico" <mc******@comcast.net> wrote in message news:<DO********************@comcast.com>...
"Aggro" <sp**********@yahoo.com> wrote in message
news:_V***************@read3.inet.fi...
> Have you tried adding indexes to columns that are used in the
search?
Depending on the table type, I think it also helps to run the analyze

command on the tables:

mysql> analyze table <tablename>;


HUZAH!
I ran "analyze" on each table, and I got back a message each time
saying "Msg_text: OK" on each one.
I guess that's good? Does analyze actually DO anything to the table or
does it just check it for problems?
Anyway, after I did that an dreloaded the querey, it still took 2
minutes.

So I indexed the columns on each table that are used in the joins, and
the next reload took only 15 seconds!! Woohoo!!

Now, I'm assuming that when you index, the benefits of indexing is
sort of like a bell-curve. The more you index the better performance,
but then you can index too much and performance decreases again and
you're simply defeating the purposes--if I understand indexing right,
yes?

So, what's an optimal amount to index? Is it just the "key" field of
each table? Any overlapping column that's part of a join? Any other
tips?

Thanks all!!
Liam
Jul 20 '05 #6
LRW wrote:
So, what's an optimal amount to index? Is it just the "key" field of
each table? Any overlapping column that's part of a join? Any other
tips?


Optimal amount is to index those fields that need indexing. If you are
unsure which of your fields need it. You can add-try-remove-try again to
find out what indexes are most needed to speed up queries. It might slow
down queries at some point if you have very much indexes, but I assume
that would take quite a lot indexes.

But remember that each time you add a index to your table, the speed to
insert rows into the table slows down a little. You don't see the
difference, unless you are inserting thousands of rows at the same time
to the table. So normally this doesn't matter.
Jul 20 '05 #7

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

Similar topics

10
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I have a 1000 line python script that takes many hours to finish. It is running with six inside 'for' loops. I've searched the net for...
12
by: dvumani | last post by:
I have C code which computes the row sums of a matrix, divide each element of each row with the row sum and then compute the column sum of the resulting matrix. Is there a way I can speed up the...
2
by: Robert Wilkens | last post by:
Ok... This may be the wrong forum, but it's the first place I'm trying. I'm new to C# and just implemented the 3-tier Distributed application from Chapter 1 (the first walkthrough) in the...
10
by: Sarah Smith | last post by:
Hello, I am a bit of a newbie to VB.NET, but not totally new. I took the plunge recently and decided (along with my colleagues), to try to convert/port a VB6 client/server app to .Net. (I'm...
11
by: Dan Sugalski | last post by:
Is there any good way to speed up SQL that uses like and has placeholders? Here's the scoop. I've got a system that uses a lot of pre-generated SQL with placeholders in it. At runtime these SQL...
0
by: rbt | last post by:
Here's a quick and dirty version of winver.exe written in Python: http://filebox.vt.edu/users/rtilley/public/winver/winver.html It uses wmi to get OS information from Windows... it works well,...
5
by: RobinAG | last post by:
Hello, I just split my database into front and back end. My front end users are experiencing really slow opening of forms. I've searched online for help speeding up forms, but I'm not sure what...
10
by: ags5406 | last post by:
I've created an application that downloads data daily from a secure web site and stores that data in an Access database. Then there are different options for allowing the user to do keyword and...
1
by: maverickman4 | last post by:
background/setup info: so we have a network set up of about 23 computers including our server which is running windows 2003 standard. we have an incredible amount of traffic going to that server due...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.