473,748 Members | 2,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2262
>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 "unnecessar y" 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******@hotmai l.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******@hotmai l.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
1840
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 and the db connection resource). The first mySqlQuery object produces just fine; but when I run it, the second mySqlQuery object (a totally different SQL query, same db connection resource) comes back null every time, even when there is data...
2
5350
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, authorid, authorname, text, posted FROM comments WHERE status = 'normal' ORDER BY posted DESC LIMIT 5; The purpose of this query is to list the five most recent (non-deleted) comments. Here is the table structure:
0
2458
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 v4.0.13-standard for pc-linux. there must be a better way to query for this information than the method i'm using, since the result with v4.0 is not what i expected, nor what i received with v3.23. i'm including sample data and queries with my...
3
7278
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 a Select and a Passthrough query in access they appear to be the same. eg. "SELECT * FROM MYTABLE WHERE MYFILED = TRUE" What do I need to do to make this a pass through query? Any help appreciated. Alex
0
2334
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 MySQL site it appears the "Query Browser" is the correct tool which I've downloaded, extracted and followed the instruction for installing it... without success, it doesn't work. To clarify what I've done I've: Downloaded:...
0
9464
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 constant DSN => "dbi:mysql:ABC:localhost"; # database username
2
10675
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 SID_SCREENING_TASKS_QUESTIONS.TASK_ID, mySQL = mySQL & "SID_SCREENING_TASKS_QUESTIONS.ID, mySQL = mySQL & "SID_SCREENING_QUESTIONS.TITLE, " mySQL = mySQL & "SID_SCREENING_QUESTION_STUDENT.Points " mySQL = mySQL & "from SID_SCREENING_TASKS_QUESTIONS,...
0
2219
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 out there could shed some light. Ok, the query (I've stripped it bare, the real query is a bit more complex)...
1
4286
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 another main and sub form. The requery on the sub form and refresh comands on the main form do not work when the form is first displayed and when the selection criteria is changed. Should I be doing a refresh and then repaint of the sub form. ...
0
8995
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
9381
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...
1
9332
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
9254
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
8252
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
6799
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
6078
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
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2217
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.