What alternatives database engines are there to MySQL?

Unsure what alternatives there are to MySQL?

Looking for increases in MySQL performance?

You might not realize that there are some compatible relational database engines. That you can drop in to replace MySQL.

You can even run SQLite without needing a server stack.

SQLite is the most widely deployed database engine in the world

Each engine has it’s own pros and cons to consider before switching from one to another. Most are available as a drop in replacement for MySQL, but yet dropping out and back to MySQL may not be as smooth. Development of new versions means the code bases and features diverge from each other.

MariaDB, Percona Server and Amazon Aurora all offer performance improvements over vanilla MySQL. All for free:

Relational Database Engine Details Costs Link
Amazon Aurora Amazon Aurora is a MySQL-compatible relational database engine that combines the speed and availability of high-end commercial databases with the simplicity and cost-effectiveness of open source databases. Amazon Aurora provides up to five times better performance than MySQL with the security, availability, and reliability of a commercial database at one tenth the cost. AWS has a free usage tier, otherwise costs depend on server spec x usage. Amazon Aurora
MariaDB One of the most popular database servers. Made by the original developers of MySQL. Guaranteed to stay open source. Free https://mariadb.org/
MariaDB subreddit
MySQL MySQL is the world’s most popular open source database. Whether you are a fast growing web property, technology ISV or large enterprise, MySQL can cost-effectively help you deliver high performance, scalable database applications. Community Edition is free. MySQL
MySQL subreddit
Percona Server Is a free, fully compatible, enhanced, open source drop-in replacement for MySQL that provides superior performance, scalability and instrumentation. Percona Server’s self-tuning algorithms and support for extremely high-performance hardware delivers excellent performance and reliability. Free Percona Server
Percona Server subreddit
SQLite SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed database engine in the world. Free SQLite
SQLite subreddit

How to join a table many times in a single SQL query statement?

Having more than one join to the same table within a query can be confusing.

More than one join can create a messy SQL statement.

If you don’t use aliases they can be difficult to read.

If your database tables need to have many columns with the same relationship to another table. You can join them with aliases in a single efficient query.

Need a working example, with sample SQL statements? Check out my other post: How to join the same table twice in a single SQL query statement?

For more about MySQL please check out the MySQL terminology guide

Interested in more SQL tips? Please subscribe in the box below, for a weekly update of my newest posts every Tuesday!

Reasons to switch from MySQL to MariaDB

Thinking of moving from regular MySQL to MariaDB?

MariaDB has a considerable performance gain over MySQL 5.6

Need any more reasons than performance to switch?

  1. Switching from MySQL to MariaDB is easy, it’s still considered a drop in replacement.
  2. MariaDB definitely has more of an open source attitude.
  3. Galera implementation is better.
  4. Maria comes by default with some distros, like the Red Hat series.

I recently found MariaDB 10 was available on my hosting environment. I saw that MariaDB was compatible with MySQL having not heard of it before then. I tried setting it up and switching was dead easy, just like MySQL and I didn’t have to make any code changes.

If you found the reasons above to consider switching to MariaDB helpful. Please comment and subscribe below so you don’t miss out on my next tips…

Pros and cons for switching to MariaDB from MySQL?

What are the advantages of switching to MariaDB from MySQL?

And are there any disadvantages?

If you like me, you may have only just heard about MariaDB. I came across it as a new option when adding a database to RackSpace Cloud hosting environment. After a quick read up about MariaDB and seeing that it was MySQL compatible. I thought why not give it a shot for the website I was working on. The website was a demo site, so there weren’t any concerns about production stability. Rackspace Cloud Hosting provided PHPMyAdmin for accessing the MariaDB. Just like you would have with MySQL. Everything appeared to look the same and work the same setting up the database. Once set-up I immediately saw that MariaDB is definitely faster than MySQL.

So what are the pros and cons for switching to MariaDB from MySQL?

Pros of MariaDB vs MySQL

  1. MariaDB has better query performance.
  2. MariaDB has more of an open source attitude.
  3. Switching to MariaDB is easy.
  4. Galera implementation is better in MariaDB.
  5. MariaDB is available as an option with some hosting environments, like RackSpace Cloud.
  6. MariaDB comes by default with some distros, like the Red Hat series.

I took a big site from MySQL to MariaDB with zero issues.

Cons of MariaDB vs MySQL

  1. Switching back from MariaDB to MySQL may not be so easy.
  2. If you are using AWS, Amazon Aurora may be even faster than MariaDB and MySQL.
  3. Your Operating System may not support MariaDB yet.
  4. Your hosting environment may not support MariaDB yet.
  5. Percona may be an even better option than MariaDB.

As with all software there are new versions in development. Each release bringing better performance with it.

So if you are looking to switch from MySQL, make sure the benefits outweigh the risks.

Wikipedia made the switch in 2013 as outlined in this blog post

Rackspace is updating all it’s Cloud Sites from MySQL to MariaDB in 2016.

Useful knowledge base articles from Maria:

If you liked this post, be sure to subscribe below and be the first to get my next post…

When do you need to use back ticks with MySQL?

Back ticks are often misunderstood.

I am trying to learn the best way to write queries.

You might not be sure how to use those funny little `ticks

`back ticks`, `backticks`

I have used single quotes, double quotes and back ticks without any real thought.

What is the standard for this?

Back ticks should be used for table and column identifiers, but are only needed when the identifier is MySQL reserved keyword.
Also if the identifier contains white space or other special characters.

When possible it’s recommended not to use keywords and special characters as table and column identifiers to avoid having to use back ticks.

If you use PHPMyAdmin to generate some queries you will see that all the column and table identifiers are escaped with back ticks by default.

For example a SELECT query would look like this:

SELECT `id`, `created_by_user_id`, `modified_by_user_id` FROM `products` WHERE 1

As none of these column and table identifiers contain any special characters and don’t match any of the MySQL reserved keywords, the same query will run just fine without the back ticks:

SELECT id, created_by_user_id, modified_by_user_id FROM products WHERE 1

Replace the back ticks with single quotes (or double quotes) and you will get an error as now each identifier wrapped in single quotes is treated as a string:

--DON'T DO THIS, AS IT WILL RAISE THE ERROR BELOW:
SELECT 'id', 'created_by_user_id', 'modified_by_user_id' FROM 'products' WHERE 1
--#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''products'

If you liked this tip, be sure to subscribe below and be the first to get my next tip…

MySQL terminology guide for newbies

MySQL Terminology helpful guide

If you are newbie to MySQL, this guide explains some terminology that is used when developing with MySQL and PHP.

This is not meant to be a definitive guide, but will be updated. Did I miss something? Please let me know or leave a comment below.

Aliases for column names Produces a more readable column name if needed. Especially when a column is produced from a function like CONCAT() for example
Aliases for table names Makes queries shorter and easier to read / write
Backticks ` MySql’s (non-standard) method for delimiting problematic identifiers
Composite Index An index made of more than one column.
Database A collection of one or more tables. Think of a Excel workbook containing many sheets.
MySQL client Software to allow to connect and manage MySQL databases. MySQL Workbench, HeidiSQL, phpMyAdmin etc…
ORDER BY clause Used to sort the results of your SELECT query by one or more columns in ascending or descending order.
Primary Key A unique identifier of a row consisting of one or more columns
Query A way of SELECTing / INSERTing / DELETEing / UPDATEing data
Results / Result set The data / result of a query being run
Row / Record A single row or record of data in a table
Schema The design of one or more tables
SQL Structure Query Language
Sub query A quested nested within another query
Table A repository of data (entity). Think of a sheet in Excel with column headers.

If you liked this guide, be sure to subscribe below and be the first to get my next post…

How to replace NULL values in a MySQL SELECT statement

Sometimes when you are SELECTing data from MySQL, some of your columns may have NULL data values and NULL’s don’t display well to end-users, so it may be best to replace the NULL’s with a string of text instead such as:

  • “Not defined”
  • “Undefined”
  • “Default value”

You could do this in PHP when you are echoing out your data, but it’s a lot easier to do it your MySQL statement.

For example if you have this simple SELECT statement, but not every manufacturers website is defined, so that the website column value is sometimes null.


SELECT id, manufacturer, website
FROM manufacturers

Then you can simply use the COALESCE function to check for and replace any NULL values with ‘Not defined’.


SELECT id, manufacturer, COALESCE(website, 'Not defined') AS website
FROM manufacturers

Also I’ve given the same column name as an alias to the function so your resulting recordset will have the same column header as your first query.

Did you find the above helpful? Please leave a comment below and let me know…

When to use USING in a JOIN rather than JOINing ON?

What is the difference between ON and USING() when JOINing two or more tables together in a SQL query?

For example you have the following two tables, Users and Products, when a Product is created, the Users id that created it is stored.

Products table:
id Primary Key
created_by_user_id Foreign Key to Users table
product_name
Users table:
id Primary Key
first_name
last_name

If you want to find out who created the product, you can do that easily with a simple SQL query like this:

SELECT p.*, u.*
FROM products p
INNER JOIN users u ON p.created_by_user_id = u.id

If however the user id field was named the same in both tables like this:

Products table:
id Primary Key
user_id Foreign Key to Users table
product_name
Users table:
user_id Primary Key
first_name
last_name

You can then use USING to JOIN the tables together and return the same data with this SQL:

SELECT p.*, u.*
FROM products p
INNER JOIN users u USING(user_id)

Each time you JOIN a table you will need to use an additional USING clause even if the second and third tables contain the exactly same column name.

Make sure that when you are JOINing tables together using the ON or the USING clause that the data type of the matching columns is the same data type.

Interested in more SQL tips? Please leave a comment below or pop your email in the box below and you’ll be sure to receive them!

How to join the same table twice in a single SQL query statement?

Having more than one join to the same table can be confusing.

You might not have even thought it was possible to have multiple joins to the same table.

It is possible and easy to do with the help of aliases.

Aliases for table names

Aliases allow you to reference the table name with an abbreviation.

For example if you have a table called “products” then you reference that table by creating an alias of “p”.

With MySQL you easily create an alias like this:

SELECT p.id FROM products p

Multiple joins to the same table can quickly create a messy SQL statement. If you don’t use aliases they can become difficult to read.

Alias SQL example:

If your database tables need to have two or more columns with the same relationship to another table. You can easily join them with the help of aliases, as explained below.

For example say you have the following two tables: Users and Products. When a Product is created, the Users id that created it is stored. Then when it’s modified the Users id that modified the record is stored.

Products table fields: Comments
id Primary Key
created_by_user_id Foreign Key to Users table
modified_by_user_id Foreign Key to Users table
Users table fields: Comments
id Primary Key
first_name
last_name

If you want to find out who created the product, you can do that with a simple SQL query like this.

In this example, we use the aliases p for the products table and u for the users table:

SELECT
p.id
, u.firstname AS 'creator_firstname'
, u.lastname AS 'creator_lastname'
FROM products p
INNER JOIN users u
ON p.created_by_user_id = u.id

If you want to find out who modified the product you can do that with a similar query using the same aliases:

SELECT
p.id
, u.firstname AS 'modifier_firstname'
, u.lastname AS 'modifier_lastname'
FROM products p
INNER JOIN users u
ON p.modified_by_user_id = u.id

But how can you combine both queries to know the user that created and modified the products in one query?

Just join the Users table twice, but you need to use a different alias each time you reference the same table.

In this example, we use the aliases u1 for the first reference and u2 for the second reference:

SELECT
p.id
, u1.firstname AS 'creator_firstname'
, u1.lastname AS 'creator_lastname'
, u2.firstname AS 'modifier_firstname'
, u2.lastname AS 'modifier_lastname'
FROM products p
INNER JOIN users u1
ON p.created_by_user_id = u1.id
INNER JOIN users u2
ON p.modified_by_user_id = u2.id

But what if a product has only been created and hasn’t yet been modified? In which case the modified_by_user_id should be null in your products table.  Then the above query won’t be able to join twice to the users table. Unless it has a user record with a null id, which it shouldn’t.  In this case you need to use LEFT JOIN like this:

SELECT
p.id
, u1.firstname AS 'creator_firstname'
, u1.lastname AS 'creator_lastname'
, u2.firstname AS 'modifier_firstname'
, u2.lastname AS 'modifier_lastname'
FROM products p
INNER JOIN users u1
ON p.created_by_user_id = u1.id
LEFT JOIN users u2
ON p.modified_by_user_id = u2.id

So now you can join the same table twice in single efficient query.

Here are all the SQL statements from above and the CREATE table statements:


For more about MySQL please check out the MySQL terminology guide

Interested in more SQL tips? Please pop your email in the box below and you’ll be sure to receive them!

MySQL phpMyAdmin Tutorial, creating tables and joining tables together

Check out my first video tutorial on YouTube.

I’m demonstrating MySQL phpMyAdmin, to get you started creating a database, creating and populating two MySQL tables and a simple SELECT statement showing how to join the two tables together.

The text size does look a little small, so the next video will be zoomed in to make it easier to see, stay tuned for more.

If you have any questions you would like answered in a video, just leave them in comments below.

Thanks,

Jon