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

query kills mySQL db

I have a query that seems to kill the db for our website (causes the
'Too many connections' error).

I would like to avoid this! Is there a way to test the query before the
db goes down? I do not have access to a staging environment. I can
access the db via Linux command line, or phpMyAdmin - however I can
only select, update, and delete.

I've heard a user can identify a bad query and kill it, how is this
done? Would my user have rights to do it (kill his own query)? How can
I tell?

Thanks -
S7

P.S. Here is the query, in-case there is something glaringly wrong:

SELECT i.id, m.name, i.weight
FROM mods m, inv i, data d
WHERE m.id = i.id AND i.id = d.id
AND d.active = 'y'
AND i.weight < 1
OR i.weight > 50

Sep 8 '05 #1
4 2217
>I have a query that seems to kill the db for our website (causes the
'Too many connections' error).
This is a strange use of the term 'kill'. If you get a 'too many
connections error trying to do the query itself, you never even
got to the query. It is possible that it loads the database
enough that it bottlenecks other queries. But then you get the error
on OTHER queries.

I would like to avoid this! Is there a way to test the query before the
db goes down? I do not have access to a staging environment. I can
access the db via Linux command line, or phpMyAdmin - however I can
only select, update, and delete.
You could try logging in from the command line BEFORE running the
query (that way you won't run out of connections). Do a 'show
processlist'. Use the same login info your query is using - you
can show and kill your own processes. Then run the query from
another connection. Keep doing 'show processlist' to see what it's
doing and if the database is still responding.
I've heard a user can identify a bad query and kill it, how is this
done? Would my user have rights to do it (kill his own query)? How can
I tell?
"show processlist" to see the ID numbers for your own processes (or
all of them with sufficient privs). "kill <id>" to kill one of
your processes. You'll get an error message if you don't have the
privs to do that. Use the same login (or an administrative one
with wide privileges) for watching the process as for running it,
that way you can see and kill it.
SELECT i.id, m.name, i.weight
FROM mods m, inv i, data d
WHERE m.id = i.id AND i.id = d.id
AND d.active = 'y'
AND i.weight < 1
OR i.weight > 50
What is your intent here? This query is equivalent to:
SELECT i.id, m.name, i.weight
FROM mods m, inv i, data d
WHERE ((((((((((((((((m.id = i.id) AND (i.id = d.id)
AND (d.active = 'y')
AND (i.weight < 1 ))))))))))))))))
OR (i.weight > 50)
If you have M mods, D datas, and II weights greater than 50, you'll
get every possible combination of them, which is at least M*D*II
results, which could be a really large number. (If M = 10,000; D=
10,000, and II = 10,000, that's a million million records, which
may not finish in your lifetime) I suspect you really want:
SELECT i.id, m.name, i.weight
FROM mods m, inv i, data d
WHERE (m.id = i.id) AND (i.id = d.id)
AND (d.active = 'y')
AND ((((((((((((((((i.weight < 1 )
OR (i.weight > 50))))))))))))))))


except you probably want more "unnecessary" parentheses.

Assuming that m.id and i.id and d.id are primary keys for the tables,
this would retrieve at most the number of records in the inv table.

Gordon L. Burditt
Sep 8 '05 #2
Hi Gordon,

Thanks for your quick response, and suggestions for monitoring/killing
the process. That makes sense.

I think my query was loading the DB, it ran, and ran. And finally the
"Too many connections" error began to show.

I had no idea my query was so bad! My intent is to find the smallest
set, where all the id's are the same (they are a sort of key), and to
show the weights where < 0 or > 50, and where they are active. Thanks
for the example - I don't understand the multiple parentheses though,
e.g.
AND ((((((((((((((((i.weight < 1 )
OR (i.weight > 50))))))))))))))))


do I need all those?

Sep 8 '05 #3
st******@hotmail.com wrote:
I don't understand the multiple parentheses though,
do I need all those?


The point is that AND binds tighter than OR, so you need to use
parentheses to make the conditions combine in the way you intend.
Gordon was being ironic by using so many nested parentheses.

Explanation of "binds tighter":

An expression of the form X AND Y OR Z is equivalent to (X AND Y) OR Z.
So the whole expression returns true every time Z is true, regardless of
the result of X AND Y.

Since your join conditions are in the position like X in this example,
they are effectively factored out, and you get every row in your tables
joined to every row in the other tables. This is called a Cartesian
product; it's rarely what you want when you're joining tables (but it is
a legitimate type of join, and has some rare good uses).

You need to parenthesize the expression like this:
X AND (Y OR Z).

I like to join using the SQL-92 JOIN syntax. It's easier to avoid
Cartesian product queries if I keep the join conditions separate from
row-restriction conditions. Your query would be written as follows:

SELECT i.id, m.name, i.weight
FROM mods m INNER JOIN inv i ON m.id = i.id
INNER JOIN data d ON i.id = d.id
WHERE d.active = 'y'
AND (i.weight < 1 OR i.weight > 50)

Regards,
Bill K.
Sep 8 '05 #4

Bill Karwin wrote:
st******@hotmail.com wrote:
I don't understand the multiple parentheses though,
do I need all those?


The point is that AND binds tighter than OR, so you need to use
parentheses to make the conditions combine in the way you intend.
Gordon was being ironic by using so many nested parentheses.

Explanation of "binds tighter":

An expression of the form X AND Y OR Z is equivalent to (X AND Y) OR Z.
So the whole expression returns true every time Z is true, regardless of
the result of X AND Y.

Since your join conditions are in the position like X in this example,
they are effectively factored out, and you get every row in your tables
joined to every row in the other tables. This is called a Cartesian
product; it's rarely what you want when you're joining tables (but it is
a legitimate type of join, and has some rare good uses).

You need to parenthesize the expression like this:
X AND (Y OR Z).

I like to join using the SQL-92 JOIN syntax. It's easier to avoid
Cartesian product queries if I keep the join conditions separate from
row-restriction conditions. Your query would be written as follows:

SELECT i.id, m.name, i.weight
FROM mods m INNER JOIN inv i ON m.id = i.id
INNER JOIN data d ON i.id = d.id
WHERE d.active = 'y'
AND (i.weight < 1 OR i.weight > 50)

Regards,
Bill K.


Thanks so much for that Bill. It's hard to spot irony when you don't
know what's going on to begin with! But I did suspect it as a
possibility ((((( ))))) just looks a little crazy. Anyway, your inner
joins worked great. The data came back quick, no cartesian products
here. Thank you both again.
S7

Sep 8 '05 #5

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

Similar topics

0
by: Phil Powell | last post by:
I have a very simple mySqlQuery object that takes two parameters: 1) the string query 2) the db connection resource I tested and was certain everything is passing correctly (the string query...
2
by: Tim Fountain | last post by:
We've recently enabled slow query logging on a server and it's proving interesting seeing which queries are bogging things down. This one is puzzling me a little: SELECT articleid, type,...
0
by: Doug Reese | last post by:
hello, i have what seems to me a very common operation i'm performing. i need to find the balance on an invoice. i was not having any problems until the production server was upgraded to mysql...
3
by: Paradigm | last post by:
I want to create a pass through query in Access to get data from a MYSQL table. I need to do this using code so that sertain aspects of the query can be changed. When I look at the SQL version of...
0
by: Murphy | last post by:
I am currently migrating a db from SQL Server & MySQL and ? (front end yet to be decided upon) As I familiarise myself with MySQL populating tables etc can be quite clumbersome. After reading the...
0
by: Philip.M.Collins | last post by:
It looks like mysql doesn't have the ability to set a query timeout. Is this true? So in the mean time I created the following perl DBI script to do it for me. #!perl # DBI service name use...
2
by: Galina | last post by:
Hello I need to get data from 4 Oracle tables and combine them into a temporary table in my application. I get data using a pass-through query dynamically created in code: mySQL = "SELECT...
0
by: taras.di | last post by:
Hi everyone, I've come across an extremely strange problem. The exact same query in both mysql command line client, and mysql query browser gives entirely different results. I was hoping someone...
1
by: tomlebold | last post by:
Having problems displaying query results from combo boxes on a sub form, which is on the same form that is used to select criteria. This has always worked form me when displaying query results on...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
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
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...

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.