哪个更好? mysql 是 LIKE 还是 REGEXP?
我在一个声明中有一个带有 LIKE 和 NOT LIKE 的named_scope。与使用 REGEXP 相比,这在执行方面是否更快?
named_scope :order_search, :order => "name LIKE '%urgent%' AND name NOT LIKE '%not urgent%' DESC, created_at DESC"
I have this named_scope with a LIKE and NOT LIKE in one statement. Is this faster in terms of execution compared to using REGEXP?
named_scope :order_search, :order => "name LIKE '%urgent%' AND name NOT LIKE '%not urgent%' DESC, created_at DESC"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是毫秒级的差异,不易察觉。除非您要处理极高的流量,否则这并不重要。
如果您的方法遇到性能问题,可能是因为您的表有大量行。瓶颈不是像那时那样(MySQL也支持REGEXP)而是你的表结构。
无论如何,您应该阅读 MySQL 索引和 MySQL 全文搜索。
It's milliseconds difference, unnoticable. Unless you're dealing with an extremely high amount of traffic, it won't matter.
If you're experiencing performance issues with your approach, it's likely because your table has a large amount of rows. The bottle-neck isn't LIKE then (and MySQL also supports REGEXP) but your table structure.
In any case, you should read up on MySQL Indexes and MySQL Fulltext Search.
据我所知,在这种情况下,MySQL 的
LIKE
将使用 Boyer–Moore,因此LIKE
可能会有一点优势。然而,与使用将整个表达式存储在
is_urgent
字段中的触发器相比,它可以忽略不计。然后,您可以在(is_urgent,created_at)
上添加索引。In so far as I'm aware, MySQL's
LIKE
will use Boyer–Moore in this case, so there might be a slight advantage forLIKE
.It'll be negligible compared to using a trigger that would store the whole expression in an
is_urgent
field however. You could then add an index on(is_urgent, created_at)
.您的查询不是最左边的查询,它无法使用索引
所以 like 和 regexp 是相同的
your query is not left most query, it could not use index
so the like and regexp are same