473,805 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.companyi d,orders.ordern um,orders.addre ss AS
o_address,order s.city AS o_city,orders.s tate AS o_state,orders. zipcode
AS o_zipcode,order s.firstname AS o_firstname,ord ers.lastname AS
o_lastname,".
"tbl_ih_account s.company,tbl_i h_accounts.addr ess AS
ih_address,tbl_ ih_accounts.cit y AS ih_city,tbl_ih_ accounts.state AS
ih_state,tbl_ih _accounts.zipco de AS
ih_zipcode,tbl_ ih_accounts.fir stname AS
ih_firstname,tb l_ih_accounts.l astname AS ih_lastname,".
"accounts.addre ss AS a_address,accou nts.city AS a_city,accounts .state
AS a_state,account s.zipcode AS a_zipcode,accou nts.firstname AS
a_firstname,acc ounts.lastname AS a_lastname, ".
"orderdate, sp_local, inhouse ".
"FROM orderstatus ".
"LEFT JOIN orders ON (orderstatus.or dernum = orders.ordernum ) ".
"LEFT JOIN tbl_ih_accounts ON (tbl_ih_account s.id = ihid) ".
"LEFT JOIN accounts ON (accounts.usern ame = accountid) ".
"WHERE submitted <> 'yes' ORDER BY orders.ordernum ASC";
Jul 20 '05 #1
6 1464
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**********@y ahoo.com> wrote in message
news:_V******** *******@read3.i net.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.companyi d,orders.ordern um,orders.addre ss AS
o_address,order s.city AS o_city,orders.s tate AS o_state,orders. zipcode
AS o_zipcode,order s.firstname AS o_firstname,ord ers.lastname AS
o_lastname,".
"tbl_ih_account s.company,tbl_i h_accounts.addr ess AS
ih_address,tbl_ ih_accounts.cit y AS ih_city,tbl_ih_ accounts.state AS
ih_state,tbl_ih _accounts.zipco de AS
ih_zipcode,tbl_ ih_accounts.fir stname AS
ih_firstname,tb l_ih_accounts.l astname AS ih_lastname,".
"accounts.addre ss AS a_address,accou nts.city AS a_city,accounts .state
AS a_state,account s.zipcode AS a_zipcode,accou nts.firstname AS
a_firstname,acc ounts.lastname AS a_lastname, ".
"orderdate, sp_local, inhouse ".
"FROM orderstatus ".
"LEFT JOIN orders ON (orderstatus.or dernum = orders.ordernum ) ".
"LEFT JOIN tbl_ih_accounts ON (tbl_ih_account s.id = ihid) ".
"LEFT JOIN accounts ON (accounts.usern ame = accountid) ".
"WHERE submitted <> 'yes' ORDER BY orders.ordernum ASC";

Jul 20 '05 #4
"LRW" <de**@celticbea r.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.companyi d,orders.ordern um,orders.addre ss AS
o_address,order s.city AS o_city,orders.s tate AS o_state,orders. zipcode
AS o_zipcode,order s.firstname AS o_firstname,ord ers.lastname AS
o_lastname,".
"tbl_ih_account s.company,tbl_i h_accounts.addr ess AS
ih_address,tbl_ ih_accounts.cit y AS ih_city,tbl_ih_ accounts.state AS
ih_state,tbl_ih _accounts.zipco de AS
ih_zipcode,tbl_ ih_accounts.fir stname AS
ih_firstname,tb l_ih_accounts.l astname AS ih_lastname,".
"accounts.addre ss AS a_address,accou nts.city AS a_city,accounts .state
AS a_state,account s.zipcode AS a_zipcode,accou nts.firstname AS
a_firstname,acc ounts.lastname AS a_lastname, ".
"orderdate, sp_local, inhouse ".
"FROM orderstatus ".
"LEFT JOIN orders ON (orderstatus.or dernum = orders.ordernum ) ".
"LEFT JOIN tbl_ih_accounts ON (tbl_ih_account s.id = ihid) ".
"LEFT JOIN accounts ON (accounts.usern ame = 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.submitte d = 'no'
order by orders.submitte d, orders.ordernum ;
Jul 20 '05 #5
LRW
"Mike Chirico" <mc******@comca st.net> wrote in message news:<DO******* *************@c omcast.com>...
"Aggro" <sp**********@y ahoo.com> wrote in message
news:_V******** *******@read3.i net.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
1988
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 ways to speed up the proccess.
12
2232
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 code in C: /* Here is the code */ // Table is "wij" int i, j; for(i = 0; i < N; ++i) {
2
1564
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 "Walkthrough" book that comes with Visual Studio .NET 2003 Enterprise Architect. My first observation is -- woah, is this thing slow. From the time I clicked "load" to the time I had a populated data set on the windows-based app was almost 5-10...
10
3164
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 not using any upgrade tools, I'm just re-writing). I have be learning/testing .net with SDI type forms and now I'm actually working on the conversion, I'm starting to worry that VB.net
11
2438
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 statements are fired off (through the C PQexecParams function, if that matters) for execution. No prepares or anything, just bare statements with $1 and friends, with the values passed in as parameters. Straightforward, and no big deal. ...
0
1092
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, but it's slow... too slow. Is there any way to speed up wmi? In the past, I used the platform and sys modules to do some of what winver.exe does and they were rather fast. However, since switching to wmi (for a more accurate representation)...
5
1440
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 the best way is with my current setup. I've inherited the database from a previous programmer, and he set things up a little uniquely. Here's the deal: I have a main form that opens, listing in a subform all of the projects, with some top-level...
10
1316
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 date searches on the database and have the information displayed for them. Everything looks and functions great, my only real dissatisfaction with my application is the update time, which in my last test took about 45-46 minutes for 9800 records....
1
2624
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 to every workstation viewing and writing data every few seconds. this is an orthodontic office that is completely paperless. charts and personal info are all on one SQL database on one server. i have pictures to manage and everything. but between...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10103
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4316
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 we have to send another system
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.