Mysql – Which query execution will be faster

join;MySQLmysql-workbenchsubquery

In the following queries, which query execution will be faster. When I tried executing them in SQLWorkbench, each time query execution time is varying.

Also, can you guys suggest some better tools for analyzing the query execution time?

Indexes are present on id's column in both the tables. The rows scan on both the tables is the same for these 2 queries.

SELECT DISTINCT sng_id FROM table1 WHERE id in (SELECT DISTINCT id from table2 WHERE LOWER(comment) LIKE '%home%')

SELECT DISTINCT sng_id FROM table1 AS a JOIN table2 AS b ON a.id = b.id WHERE LOWER(b.comment) LIKE '%home%'

Best Answer

In normal circumstances the JOIN is the structure that would be recommended for performance reasons. However, it looks like you're attempting some kind of search functionality.
Btree indexes don't work well with the kind of filtering you're implementing:

WHERE LOWER(b.comment) LIKE '%home%'

Using a function on the data (LOWER) will render any index in place useless as well as the wildcard filtering %string%. The query will likely run okay on small tables, but will suffer the longer it gets more rows that are scanned. This access type is eased with Fulltext indexing and relative matching rather than table scanning.

You might also try using a dedicated search product with inverted indexing like Elasticsearch, Solr or Sphinx.