Published 18 years 9 months ago • Last updated March 22, 2025 • ⏱️ 2 min read
Continuing from my earlier post on PHP performance, I thought I'd share a few Mysql tips that I've learnt over the years. Hope it helps someone and please leave a comment with your own tips or provide any corrections to the ones mentioned.
(Fastest)
2.<pre lang="sql">SELECT * FROM table WHERE MATCH (field) AGAINST ('+Keyword' IN BOOLEAN MODE)</pre>
(Fast)
3.<pre lang="sql">SELECT * FROM table WHERE RLIKE '(^| +)Keyword($| +)'</pre> OR
<pre lang="sql">SELECT * FROM table WHERE RLIKE '([[:space:]]|[[:<:]])Keyword([[:space:]]|[[:>:]])'</pre>(Slow)
(Fastest)
(Fast)
(Slow)
(Slow)
SELECT SQL_CALC_FOUND_ROWS * FROM table WHERE Condition LIMIT 0, 10 SELECT FOUND_ROWS() </pre>
(Fastest)
2.<pre lang="sql"> SELECT * FROM table WHERE Condition LIMIT 0, 10 SELECT COUNT(PrimaryKey) FROM table WHERE Condition </pre>
(Fast)
$result = mysql_query("SELECT * FROM table", $link); $num_rows = mysql_num_rows($result); </pre> (Very slow)
Use an INNER JOIN when you want the joining table to only have matching records that you specify in the join. Use LEFT JOIN when it doesn't matter if the records contain matching records or not.
<pre lang="sql"> SELECT * FROM products INNER JOIN suppliers ON suppliers.SupplierID = products.SupplierID </pre>Returns all products with a matching supplier.
<pre lang="sql"> SELECT * FROM products LEFT JOIN suppliers ON suppliers.SupplierID = products.SupplierID WHERE suppliers.SupplierID IS NULL </pre>Returns all products without a matching supplier.