Optimizing Queries on MYSQL (InnoDB tables) - mysql

I got a problem. I need to (for didactical purposes) show the optimization process of a query (example: 2 seconds without any index, 1 second with an index.. etc).
I have a mysql database with 12 tables and 1.000.000 records in each table.
Problem is: If I execute a query first time, it takes x seconds. The second execution of the same query, takes ALWAYS 0 seconds. I tried flushing tables, flushing query cache, setting query cache to OFF in the select, setting inno_db_buffer_pool to 0M.. nothing. After the first execution of the query, MYSQL caches somewhere (I think) the result, so the next execution takes always 0 seconds.

If you need to optimize your queries, use SQL_NO_CACHE in your SELECT statements.

You should use SQL_NO_CACHE when you run query first time to tell MySQL not to put the result into the cache. Applying SQL_NO_CACHE only after performing some query doesn't make sense. Reset your server and perform all queries with SQL_NO_CACHE prefix.

Related

How to reduce select * from table_name query execution time for table with 200k+ records?

I have mysql table set in RDS and I am querying from my web application. The query itself takes more than 20 seconds while other customized operations takes less than 0.5 seconds combined. Is there any way to reduce the query execution time to 2-3 seconds?
I tried indexing but it doesn't help either.
$table_data = select * from table_name
I expect the above query to take as less time to execute as possible.
Here's the thing:
Your query has no filtering predicates (no WHERE clause). Then you are selecting all the rows of the table.
Your query has a * instead of a subset of columns. Therefore, it's selecting all the columns of the table.
In sum you are getting the whole table, every time.
There's nothing you can do on the SQL side. Your chances now reside on the admin side of the database. Get more hardware (faster CPU), increase paralellism (more cores), a faster SSD maybe?
Alternatively, you could use caching on your app side. Run the query only once every 5 mins, and keep the rows in memory.

MySQL query with derived Table Query runs faster than without it

I have a MySQL Query.
When I run without derived table, the execution time is about 6 sec. However, if I run with derived table, the execution takes less than 1 sec. I have seen the EXPLAIN SELECT PLAN of the query. I do not get much out of it. I can't add indexes on tables or use view or procedures.
However, I am not sure which query to move with derived query or simple query. AND does I need to consider the EXPLAIN result, or the actual execution time for selection of best option.
Yes, you should consider both the EXPLAIN output and the actual execution time.
I have lots of queries that have inline views ("derived tables" in MySQL parlance). And some of those run much faster than alternate queries that return equivalent results, but which don't use inline views.

Why does the first execution of a mysql query with no query cache takes longer time than later executions?

I am trying to optimize a query of the form SELECT SQL_NO_CACHE col FROM TABLE ... When I first connect to the database and execute the query it takes about 9 seconds. When I execute the query the second time it takes almost 0.1 seconds. I place the SQL_NO_CACHE in the query to ensure that mysql is not reading the result from cache. My question is why does the first execution of the query, right after the connecting to the database (mysql -uroot ... ) takes significantly longer than subsequent executions. What is the actual execution time of the query?
MySQL can take a while to warm up its internal caches. Remember, SQL_NO_CACHE means avoid the query cache only. The index cache is the most important from a performance perspective. If the index has not been read, there's a significant penalty the first time it's used.
If you're using InnoDB, which you should be, ensure that your buffer pool is sufficiently large. Most servers should allocate at least several GB of memory.

Mysql query run twice is much faster the second time even with SQL_NO_CACHE

I am trying to profile two different queries that do the same thing to find which one is faster. For testing, I have put SQL_NO_CACHE into both queries to prevent the query cache from messing up the timing.
Query A is consistently 50ms.
Query B is 100ms the first time it is run and 10ms if I run it a second time shortly after.
Why is Query B faster the second time? The query cache should not be speeding up the queries. Could it be that the first run of query B loads the data from disk into memory so that the second query is running in memory and faster? Is there a way to test this? I tried to test this myself by doing select * from the table before I ran Query B, but it still exhibited the same behavior. Is SQL_NO_CACHE perhaps not working to disable the query cache?
Query B looks something like this:
SELECT SQL_NO_CACHE foo,bar FROM table1 LEFT JOIN table2 ON table1.foo=table2.foo WHERE bar=1
Depending on the storage engine you're using, yes it is most probably being loaded from a data cache and not a query cache.
MyISAM provides no storage engine level caching for data, and only caches indexes. However, the operating system often serves up data from its own caches which may well be speeding up your query execution.
You can try benchmarking the query in a real scenario, just log that specific query to the database every time its executed (along with its execution time).
Depending on the size of your indexes and your table type, it may be that indexes are not in memory the first time the query is run. So MySQL will pull indexes into memory the first time the query is run, causing a significant slowdown. The next time, most of what MySQL needs may in memory, resulting in the performance gain.
Is your app making a connection and doing the authentication handshake on the first query? If so the 2nd query will already have an open/authenticated connection to execute from. Try running it a 3rd time and see if the 2nd and 3rd tries are close to the same time.

MySQL single query benchmarking strategies

I have a slow MySQL query in my application that I need to re-write. The problem is, it's only slow on my production server and only when it's not cached. The first time I run it, it will take 12 seconds, then any time after that it'll be 500 milliseconds.
Is there an easy way to test this query without it hitting the query cache so I can see the results of my refactoring?
MySQL supports to prevent caching single queries. Try
SELECT SQL_NO_CACHE field_a, field_b FROM table;
alternatively you can diasble the query cache for the current session:
SET SESSION query_cache_type = OFF;
See http://dev.mysql.com/doc/refman/5.1/en/query-cache.html
To add to johannes's good answer, what I do is
RESET QUERY CACHE;
This has the slight added advantage of not requiring any changes to either the statements I'm executing or the connection.
A trivial thing to do is to alter the statement you're executing somehow, such as put a random number in a comment, because a queries are located in the cache only if they are byte-identical to some previous query.