Make sure that JOIN columns both carry indexes of the same type

Last updated on
8 September 2016

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Running a JOIN on two columns of different data types may force a sequential scan.

Example:

SELECT * 
FROM foo
INNER JOIN bar ON foo.a = bar.b

If foo.a and bar.b are not of the same data type, the query planner will need to cast the data of each foo.a and bar.b entry. For instance, if foo.a is integer and bar.b is string, it can't use one to lookup in the other unless and until it casts to the right type. This happens using any database, inducing MySQL and PostgreSQL.

Even if the cast happens on memory only, it will consume CPU time, at least one cycle per row and hence will be very slow. On very large tables, this should be avoided.

More examples soon.

Help improve this page

Page status: No known problems

You can: