Is there a significant performance result while using 'string field with cast' in where
clause when trying to filter or sort result instead of using int field at the beginning? because I want meta contains string value too,
I'm thinking about separating meta string and meta int in separate tables or just using one string table with cast for int value.
Just like wp_postmeta works?
META TABLE ==
meta_id | post_id | meta_key | meta_value
1 | 101 | quantity | 8
2 | 101 | price | 100
3 | 102 | quantity | 7
4 | 102 | price | 56
5 | 103 | quantity | 12
6 | 103 | price | 256
POST TABLE ==
post_id | about
101 | Pencil | Luxurious pencil only for you
102 | Eraser | All your mistakes, gone!
103 | Pen | Unrivaled penmanship, stronger than sword.
Query :
select
p.post_id,
p.name,
p.about,
m1.meta_value,
m2.meta_value
from post_table p
inner join meta_table m1
on m1.post_id = p.post_id and m1.meta_key = 'quantity'
inner join meta_table m2
on m2.post_id = p.post_id and m2.meta_key = 'price'
where CAST(m1.meta_value as int) < 10
order by CAST(m1.meta_value as int) asc
Thank you
From the Index documentation :
Comparison of dissimilar columns (comparing a string column to a temporal or numeric column, for example) may prevent use of indexes if values cannot be compared directly without conversion.
In your case, the expression CAST(m1.meta_value as int) < 10
will likely not use any index that could be used to satisfy that predicate, because you are not referencing meta_value in its defined type.
When columns are used to store multi-type data (strings, ints, dates, etc.), these are the types of problems that come up. Usually, using the right data type for the data allows the db to work as it is intended, and for the db to make assumptions that can allow for optimal query execution.
If you'd like to tune this particular query, then consider running EXPLAIN select ...
and analyzing or posting the result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.