How to debug slow mysql - mysql

I have an SQL query that takes 7 seconds to run on one computer, but on another computer (identical hardware, database has been copied over with mysqldump and so is the same), that same query runs for over 2000 seconds.
How do I find out why this is? All the advice I can find online about debugging slow mysql seems to boil down to 'find the slow queries'. That doesn't help me here. Show processlist doesn't show any other queries running, so why is it taking hundreds of times longer to execute this one on one computer than another?

What I understand is that your SQL Server is same in BOTH conditions. What you are saying is that when query x is executed from client1 it takes few seconds; while from other client client2 it is taking more than 2000 seconds.
I feel problem is network between client2 and your database server. Try to to ping between these two servers. It should give you some hint. This theory is also supported by the fact that you mentioned that server is NOT showing query getting executed.
If in both conditions SQL Servers are different; while indexes etc. are identical ; then problem is that ANALYZE has not been executed on client2 since long time.

Related

MySQL server taking minutes to answer

I have a server and it's systems use NodeJS and MySQL.
When I get a lot of users, the MySQL server takes minutes to answer to the querys.
I have tried everything I could find, nothing worked.
Its just a simple query to see if the user is logged or not, and the server takes minutes to answer, when I make a query trough the SSH terminal (mysql -p) it takes less then 1 second, when I make a query trough MySQL it takes less then 1 second too.
The mysql server is not too overloaded, it gets less then 50 querys/s, the VPS its running have spare RAM, CPU, SSD, etc.
The problem happens in every query that the login servers make, to see if the user is logged, to get information about him, to buy something in the shop.
Thanks so much.
If you need to check the query, here is a query to get information about a test user:
http://maq-1.agariohub.net:4040/info/eyJ0b2tlbiI6IjVyeHU4dnFCQklraklYcjljTzV2Mldsc3FhWGZUdHkySzV6ektSdGg1ZllXYnkyZjYxRVZQUm9VdXFRWE1ZZ2QifQ==
The answer is a JSON, in the end of it it shows how much time it took to perform the two querys the server makes

MySQL Workbench kicks me off before query runs

I'm using MySQL workbench to run a query. The query is reasonably big. When I limit the results to say 50 records the results are as expected. But when I remove the limit, the query runs for ~ 5 minutes then prompts me to re-enter my password. After doing so I see the query has not run and has stopped working.
It seems like there is a setting that kicks me off after a certain amount of time but I cannot see it in the drop down menus.
Has anyone experienced this? Any advice?
** Update**
Query ran and Workbench says results returned but they are not and I got the screen shot error pop up here:
It sounds like one of three things are the likely culprit.
MySql Workbench may be timing out.
This may help with #1
Go to Edit -> Preferences -> SQL Editor and set to a higher value this
parameter: DBMS connection read time out (in seconds). For instance:
86400.
Close and reopen MySQL Workbench. Kill your previously query that
probably is running and run the query again.
The mySQL server is timing out
You will need access to the server's timeout configuration
Something network related (but far less likely if your timeout happens consistently at around 5 mins)
There are a number of factors that might need adjusting.
If #2, you will need access to the server's timeout configuration
If #3, well, there are a number of factors that might need adjusting.

Execution time is different for the same query. What should be the reason?

When I execute the same query multiple time in Mysql console. Execution times vary all the times.
I can understand the difference if its in milliseconds. But sometimes same query take 1 second and sometimes same query take 5 seconds.
What should be the reason in this case ?
MANY reasons:
the result was cached and the cache got cleared
the table is locked (maybe because it is executing another big query)
the disk is slow or busy doing other things
you are running out of memory
the results might be changing (pull 1k record vs pulling 500k records)
the Server is remote, so you might have network problems
it is the ghost in the machine

Queries executing very slowly

I have an sql... It's a "SELECT". I can't show it, but it has 5 unions and a lot of joins (inner and left). I have also created all the necessary indexes. On the local machine it takes less then a second (~ 0.5 s) to get the results. But on the server it executes very-very long time.
Databases on the local machine and on the server are identical. I've recently dumped the server database and restore it on the local machine.
About 35 minutes ago I launched an "EXPLAIN" of this sql and it is still running. Also I see "Copying to tmp table" label for that explain in the process list.
All the tables are optimized.
I tested with MyISAM and InnoDB engines.
The server load average is less than 1, MySQL is not under load too.
It might be important - server on a cloud service. I have no access to the cloud statistics - just use the server.
What can you suggest me?
I found out the reason.
As I said before (in the comments) I did EXPLAINs for every subqueries and noticed some differences with the same EXPLAINs on the local machine (for 2 of 5 subqueries).
It solved by creating additional indexes.
Different machines - different results. I expected that it would be some differences, but I could not even did the EXPLAIN. That's a strange. Helped only the partial EXPALINs.
Thanks for all.

MySQL Connection Limit Advice

I've hit a problem with my MySQL queries and was hoping someone could offer some help/advice.
I'm developing a PHP-based system which combines quite a lot of data in different tabs on one page (tab1 = profile, tab2 = address, tab3 = payments etc.) and as a result, one page can have up to 34/40 MySQL queries pulling from different tables or with different criteria.
The page load became really slow and I asked my web host if they knew what was wrong and they advised it's because of slow MySQL queries (some over 2 seconds). They also said that my MySQL user is only allowed 15 connections at a time.
If my page has 40 queries and only 15 connections are allowed at a time, does this mean they effectively queue and wait for one to complete? If this was the case then I can understand why the page is taking a while to load but i'm not sure of the solution. Is 15 MySQL queries considered a lot or is this quite a tight restriction by my host (HostMonster)?
Also, if there were 15 users accessing the system at the same time, would this 15 connections be split between each of them or is it 15 connections per user logged into the site? I assume they mean per database user but all people who access the system will be using the same database user so it seems impossible to create a system in which several users can access at one?
The whole connections thing has confused me a little.
Thanks in advance for any help!
Have one connection per page. Put the queries into sequence
Optimize those queries - see explain and use indexes
Perhaps combine queries to reduce the through put.
BTW 10+ queries per page is excessive IMHO.
If having max connect error problem then use this command from command line
mysqladmin flush-hosts -uuser -p'password'
this will flush hosts that MySQL has recorded and will build the list again. In the newer version of MYSQL 5.6 you get more information on this but not on previous version.
You can set the following
max_connect_errors 10000
to avoid the message to appear again.
15 queries or connection is not a problem at all, in busy databases we have seen thousand connections and tens of thousands of queries per second.