Understanding why MySQL chooses a particular join order or why
table scan is chosen instead of range scan is often very hard
even for experienced MySQL users. Two almost identical queries,
differing only in constant values, may produce completely
different plans. That's why we're introducing a great new feature
in 5.6: Optimizer Tracing. The target users of
this feature are developers and MySQL users experienced enough to
understand the ins and outs of EXPLAIN.
What Optimizer Tracing is
You may already have guessed this, but optimizer tracing is a
printout of important decisions the MySQL optimizer has
done during the process of making the Query Execution Plan.
The trace is presented in JSON format which is easy to read both
for humans and others.
Currently, the optimizer trace includes …
Assume you have a table where you store non-overlapping
intervals using two columns, e.g. IP ranges. IP ranges are
simple to represent using integer notation:
CREATE TABLE ip_owner ( owner_id int NOT NULL,
/* some columns */ ip_start_int bigint
NOT NULL, /* IP address converted
to integer */
ip_end_int bigint NOT
NULL, /* IP address
converted to integer */
PRIMARY KEY (owner_id), INDEX ip_range
(ip_start_int, ip_end_int) ) ENGINE=InnoDB;
And then you find yourself in a situation where you want to know
who, if anyone, owns the IP address X. This can be done using the
following query:
SELECT * FROM ip_owner WHERE ip_start_int <= X AND ip_end_int …
The range access method uses an index to read a subset of rows
that form one or multiple continuous index value intervals. The
intervals are defined by the query's range predicates, which are
comparisons using any of =, <=>, IN(), IS NULL, IS NOT
NULL, >, <, >=, <=, BETWEEN, !=, <> or
LIKE.
Some examples:
SELECT * FROM blog WHERE author_id IN (1, 7, 8, 10) SELECT * FROM
orders WHERE value > 1000
You know that the range access method is used
when EXPLAIN shows type=range.
Naturally, there has to be an index on the column used by the
range predicate. Since indexes are ordered, MySQL will, for each
interval, dive down the index using the interval start value and
read it's way through the index leaves until it reaches the
interval end value:
…
In my previous article I’ve shown how to get rank using mysql query. Now I’m showing how to retrieve result based on today, week and month using mysql query. Actually I also implemented this in my quiz project so I’m sharing this with you.
For the table structure please look my previous article http://thinkdiff.net/mysql/how-to-get-rank-using-mysql-query/
Task 1: I’ve to retrieve those users rank who played the game
today.
Solution: Look at the query
SELECT uid, participated, correct, wrong from quiz_user WHERE DAYOFMONTH(CURDATE())=extract(day from updated) ORDER BY correct DESC, participated ASC limit 30
So the above query returns the result of those users who played today. Here
CURDATE() …[Read more]