473,786 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

i suck, but would like to do something complicated with 4.1

i suck so much that i don't even know if this is a JOIN or a subquery or
who-knows what. Here's the idea:

I want to select two things at the same time (form one table)
average for columnX
and
average for columnX where columnY=Z

so i started of course with
select avg(columnX) as avg1, avg(columnX) as avg2 from table where columnY=Z

which of course gives me the same thing twice.
i want
select avg(columnX) as avg1, avg(columnX) as avg2 from table where columnY=Z

so i head in this direction:
select avg(m1.columnX) as avg1, avg(m2.columnX) as avg2 from table where
m1.columnX<>'' and m2.columnY=Z

does this sound right? I just ran it and i got two different numbers, but
-it took forever (it's a big table)
and
-i haven't checked the math by hand yet.

Any tips for speed?
Jan 15 '06 #1
4 1626
what I actually went to was

select avg(m1.columnX) as avg1, avg(m2.columnX) as avg2 from table m1,
table m2 where
m1.columnX<>'' and m2.columnY=Z

and i checked it and it looks right... i need to maximize speed though,
because i'll actually be calculating averages on several fields in this way.

How much would it slow down the inserts to build an index on EVERY field
in the table?
Jan 15 '06 #2
"Matthew Crouch" <ma************ @spamlessverizo n.net> wrote in message
news:iVwyf.9523 $Di.3957@trnddc 06...
I want to select two things at the same time (form one table)
average for columnX
and
average for columnX where columnY=Z
Here's how I'd do it:

SELECT AVG(m1.columnX) AS avg1, AVG(m2.columnX) AS avg2
FROM myTable AS m1 LEFT OUTER JOIN myTable AS m2
ON m1.primaryKey = m2.primaryKey AND m2.columnY = Z

This works because AVG() ignores rows where the field has a NULL state, and
the columnY condition in the ON clause makes the join leave out some rows of
the right-hand-side of the join, replacing them with NULL fields.

Note that if you have more computations to make, you'd make an additional
left outer join for each one. MySQL has a practical limit to the number of
joins you can do in a single query, usually 31.
Any tips for speed?
Do the separate computations in separate SQL queries. I don't know why so
many people on newsgroups insist on doing all their computations in a single
SQL query. It makes one's code a lot more complicated. Whomever takes over
the project after you're gone will curse your name.

So this is _really_ how I'd do it:

SELECT AVG(m1.columnX) AS avg1
FROM myTable

SELECT AVG(m2.columnX) AS avg2
FROM myTable AS m2
WHERE m2.columnY = Z

See how much simpler? And its easier to add new computations.
How much would it slow down the inserts to build an index on EVERY field

in the table?

That might not matter. If you need the query to be faster than it can be
without those fields indexed, then you've got to pay the cost of maintaining
the indexes (if you need both inserts and computations of averages to be at
top speed, then consider pre-calculating the averages and storing them in
another table).

You should also know that MySQL can use only one index per table in any
given query. So if it's using the index on the primary key field (e.g. for
the join), then you might as well not index columnX or columnY.

Try the queries with and without the indexes created, and use the EXPLAIN
statement to help you understand if the indexes will help or not.
http://dev.mysql.com/doc/refman/5.0/en/explain.html

Regards,
Bill K.
Jan 15 '06 #3
Bill Karwin wrote:
"Matthew Crouch" <ma************ @spamlessverizo n.net> wrote in message
news:iVwyf.9523 $Di.3957@trnddc 06...
I want to select two things at the same time (form one table)
average for columnX
and
average for columnX where columnY=Z

Here's how I'd do it:

SELECT AVG(m1.columnX) AS avg1, AVG(m2.columnX) AS avg2
FROM myTable AS m1 LEFT OUTER JOIN myTable AS m2
ON m1.primaryKey = m2.primaryKey AND m2.columnY = Z


Well, I was trying this out on one field, but i actually need
averages/sums for almost all of the fields in the table, so i took your
advice

this calculated 2 different averages on 8 different fields and came back
in less than a second -- 20,000 records.

This table is only likely to grow to 50,000 records over the life of the
project -- so if this checks out, i won't need any weird
optimizing/indexes at all. This is the largest table I've personally
worked with, but MySQL doesn't seem to be batting an eye...
Note that if you have more computations to make, you'd make an additional left outer join for each one.


i didn't do this 'cause i wasn't sure what you meant, but it worked
anyway. my query was like

SELECT
avg(m1.scale) as zscale, avg(m2.scale) as pscale,
avg(m1.bf_perce nt) as zbf_percent, avg(m2.bf_perce nt) as pbf_percent,
avg(m1.scc) as zscc, avg(m2.scc) as pscc,
avg(m1.spc_agen cy) as zspc_agency, avg(m2.spc_agen cy) as pspc_agency,
avg(m1.lpc_agen cy) as zlpc_agency, avg(m2.lpc_agen cy) as plpc_agency,
avg(m1.sediment _agency) as zsediment_agenc y, avg(m2.sediment _agency) as
psediment_agenc y,
avg(m1.temp) as ztemp, avg(m2.temp) as ptemp

FROM table AS m1
LEFT OUTER JOIN table AS m2
ON m1.pkey=m2.pkey
AND m2.producer_id= '34567'
How much would it slow down the inserts to build an index on EVERY field


in the table?

That might not matter. If you need the query to be faster than it can be
without those fields indexed, then you've got to pay the cost of maintaining
the indexes (if you need both inserts and computations of averages to be at
top speed, then consider pre-calculating the averages and storing them in
another table).

You should also know that MySQL can use only one index per table in any
given query. So if it's using the index on the primary key field (e.g. for
the join), then you might as well not index columnX or columnY.

Try the queries with and without the indexes created, and use the EXPLAIN
statement to help you understand if the indexes will help or not.
http://dev.mysql.com/doc/refman/5.0/en/explain.html

Regards,
Bill K.

Jan 15 '06 #4
"Matthew Crouch" <ma************ @spamlessverizo n.net> wrote in message
news:Z7zyf.8565 $US3.4817@trndd c04...
Note that if you have more computations to make, you'd make an
additional left outer join for each one.


i didn't do this 'cause i wasn't sure what you meant, but it worked
anyway.


Ah -- never mind me, I misunderstood when you said you wanted to do this on
several fields. I thought you meant that you want to calculate averages on
columnX, based on different subsets of rows. So different averages for
columnY = Z1, for columnY = Z2, for columnZ >= 1234, etc. But that's not
what you meant. :-)

Regards
Bill K.
Jan 16 '06 #5

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

Similar topics

32
3208
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
20
2408
by: Dan Vande More | last post by:
Hey list, I'm just wondering if anyone can point me in the direction of a mirror that doesn't suck. I generally don't do alot with postgres other than downloading and installing the newest releases. Everytime there is a release, it takes forever for it to show up on an ftp server. This is in the case that the ftp server even works. Now in a case where onelikes to browse with the browser of their choice, it totally blows becauseit's _so_...
7
2137
by: MLH | last post by:
I would like to read a MAC address from at least one NIC in a PC on which I have an Access 97 runtime app installed. What's the least hassel method you know of?
10
2607
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results other than straining our relationship... I just downloaded Visual C# Express Edition to mess with .NET 2.0 generics. Being a numerically inclined developer, the first thing I wanted to write was a generic complex number class. I also have some...
93
7200
by: Dave | last post by:
I am a VB.NET guy, recently i tried to write program using C#, having being pampered by VB.NET's IntelliSense i find that the C#'s IntelliSense is SUCK!
458
21482
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers simply aren't smart enough to understand C++? This is not merely a whimsical hypothesis. Given my experience with Java programmers --- the code they write and the conversations they have --- Occam's Razor points to this explanation. For example,...
31
2546
by: Bill Norton | last post by:
Over at PCMag.com there is a debate going on over the usability of CSS. The discussion was initiated by an article by John Dvorak called "Why CSS Bugs Me" (http://www.pcmag.com/article2/0,1759,1987181,00.asp). The discussion starts here: http://discuss.pcmag.com/forums/1/1004331343/ShowPost.aspx#1004331343. Someone suggested that some of you folks might have a thing or two to say about the topic, so I'm inviting everyone to drop by and...
6
1650
by: Slonopotam845 | last post by:
Fat cock exploring on this crazy movie ;) ---------------------------------- http://community.ihostasp.net ASP.NET Developer Community
7
2549
by: adfghergaer | last post by:
First of all, I'm not a programmer. I'm here because I had an idea for a software program that my employer may hire someone to design, and I'm wondering what kind of investment it would take to hire someone to design it. (Thousands of dollars? Tens of thousands? Hundreds of thousands?) We want to create a software program that Home Inspectors would use to create their inspection reports. The concept of the program is somewhat similar to Turbo...
0
9650
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...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8992
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...
1
7515
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
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...
1
4067
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
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.