A
A
apptimeru2016-09-13 21:09:20
SQL
apptimeru, 2016-09-13 21:09:20

How to optimize sql query? And how to determine which request is better?

Good afternoon, please tell me how to optimize this query

SELECT terms.name, terms.slug, tags_meta.id_tags FROM terms 
LEFT OUTER JOIN term_taxonomy ON terms.term_id = term_taxonomy.term_id AND term_taxonomy.taxonomy = 'post_tag'
LEFT OUTER JOIN term_relationships ON term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id
LEFT OUTER JOIN tags_meta ON tags_meta.id_tags = terms.term_id
WHERE term_relationships.object_id = $param1 AND tags_meta.id_tags != ''

This is a standard Wordpress database. it is necessary to pull out all the tags for the post, but the task is complicated by the fact that there is another table that additionally filters the tags (tags_meta). This query works fine, but I don’t know how difficult it is for the database.
Can split it into multiple requests? For example, to get all the tags of a post, and then additionally filter through another table, you get 2 queries, but which is better?
Or maybe the same query can be written in a more elegant and optimized way. In general, how can you check which query is running faster and with less load? Is there any way to compare requests?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SharuPoNemnogu, 2016-09-15
@SharuPoNemnogu

The select involves fields from the terms and tags_meta tables, hence the question why left join term_taxonomy and term_relationships. Describe what is in which table and what you want to get as an output.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question