Archive for the ‘Cool MySQL Queries’ Category

MySQL Left Join

Июль 6th, 2011
Even if you think you know everything about LEFT JOIN, I bet you will learn something or two in this post! A reminder about “A LEFT JOIN B ON conditional_expr” The ON condition (in the expression “A LEFT JOIN B ON conditional_expr”) is used to decide how to retrieve rows from table B (Matching-Stage). If [...]
PlanetMySQL Voting: Vote UP / Vote DOWN

The battle between FORCE INDEX and the QUERY OPTIMIZER

Октябрь 26th, 2010
Query Optimizer is a part of the server that takes a parsed SQL query and produces a query execution plan. MySQL Query Optimizer uses (as one of its parameters) the stored key distribution (Cardinality) mechanism to determine the order in which tables should be joined, when you perform a join on something other than a [...]
PlanetMySQL Voting: Vote UP / Vote DOWN

Optimizing the MySQL IN() Comparison Operations Which Include the Indexed Field

Октябрь 7th, 2010
The MySQL IN() Comparison Operator is said to be very quick if all the values are constants (the values are then evaluated and sorted first, and the search is done using a binary search). However, what if the field which the IN clause refers to, is part of the index used to execute the query? [...]
PlanetMySQL Voting: Vote UP / Vote DOWN

Limited SELECT count(*)

Май 14th, 2010
A SELECT statement with COUNT returns the number of rows retrieved by the SELECT statement. For performance, the desired result is to limit that count. Including a LIMIT clause in the SELECT statement will not work since it only restricts the number of rows returned, which is always one. The solution, what I call “Limited-Count”, is done [...]
PlanetMySQL Voting: Vote UP / Vote DOWN

Conditional Joins in MySQL

Февраль 7th, 2010
One way to do a “Conditional Join” in MySQL is by using a “LEFT JOIN”.  Create a “LEFT JOIN” for each condition and combine the results into one column using an “IF” statement by the “SELECT” expression.  Here’s an example: Suppose you have three tables: questions: a table consisting of question ids, timestamps, and whether or not [...]
PlanetMySQL Voting: Vote UP / Vote DOWN

Selecting records from one table that dont have specific relationship in a second table

Январь 17th, 2010
  Question: How can you select records from one table that doesn’t have a specific ID in a second, many-to-many table?   In other words, imagine you have a questions table (A), categories table (B), and a many-to-many relationship link table (L).  This setup allows a question to have several categories.  How can you find questions that don’t [...]
PlanetMySQL Voting: Vote UP / Vote DOWN