mysql 内连接语句和内存过度使用!
我运行下面的查询从 3 个 mysql 表中获取,它工作正常,但它增加了内存使用量,有时需要 10 秒才能开始加载页面
INNER JOIN table_tags AS ntg ON (ns.tags = '' OR CONCAT(' ',COALESCE(ns.tags,' '),' ') LIKE CONCAT('% ',ntg.tid,' %'))
INNER JOIN table_topics AS nto ON (ns.associated = '' OR CONCAT(' ',COALESCE(ns.associated,'-'),' ') LIKE CONCAT('%',nto.topicid,'%' ))
问题 处于 Inner Join statemnet 中,如果我删除
ns.tags = '' 或
和
ns.associate = '' 或
内存过度使用问题将得到解决,但无法显示带有空标签字段的故事
是否有其他方法可以编写以下语句来包含所有故事,甚至那些没有标签的故事!?
INNER JOIN table_tags AS ntg ON (ns.tags = '' OR CONCAT(' ',COALESCE(ns.tags,' '),' ') LIKE CONCAT('% ',ntg.tid,' %'))
我的标签 id 存储在 table_ns 中,如 ( 14 17 2 ) 以空格分隔
im running bellow query to fetch from 3 mysql tables and it works fine but it increases memory usage and sometime it takes 10 seconds to start loading the page
INNER JOIN table_tags AS ntg ON (ns.tags = '' OR CONCAT(' ',COALESCE(ns.tags,' '),' ') LIKE CONCAT('% ',ntg.tid,' %'))
INNER JOIN table_topics AS nto ON (ns.associated = '' OR CONCAT(' ',COALESCE(ns.associated,'-'),' ') LIKE CONCAT('%',nto.topicid,'%' ))
problem is in Inner Join statemnet and if i remove
ns.tags = '' OR
and
ns.associated = '' OR
the memory overuse problem will be fixed but cant show stories with empty tag field
is there any other way to write bellow statement to include all stories even those with no tags !?
INNER JOIN table_tags AS ntg ON (ns.tags = '' OR CONCAT(' ',COALESCE(ns.tags,' '),' ') LIKE CONCAT('% ',ntg.tid,' %'))
my tags id are stored in table_ns like ( 14 17 2 ) seprated with space
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的条件
ns.tags = '' OR ...
意味着如果ns.tags = ''
为 true,则 table_stories 中的此记录将与 all< /em> table_tags 中的记录。与ns.linked = '' OR ..
相同。这可以轻松地“创建”大量结果集,但这很可能不是您想要的。如果您真的不想/无法更改/规范化表结构(正如您在对我之前的答案的评论中所述),我最好的猜测是使用两个 LEFT JOIN 类似:
但是 MySQL 不能为此使用索引,因此查询将导致全表扫描。它还需要临时表和文件排序,即查询相对较慢。
在不改变表结构但将数据格式从
1 2 3
更改为1,2,3
的情况下,你至少可以摆脱< code>Concat() 和LIKE
通过使用稍快的 FIND_IN_SET(str, strlist)。只是为了完整起见,以防您改变主意: http://en.wikipedia.org/wiki/Database_normalization
Your condition
ns.tags = '' OR ...
means that ifns.tags = ''
is true this record from table_stories will be joined with all the records in table_tags. Same withns.associated = '' OR ..
. This can easily "create" huuuge result sets and that's most likely not what you want.If you really don't want/can't change/normalize the table structure (as you've stated in a comment to my previous answer) my best guess is to use two LEFT JOINs like:
But MySQL can't use indices for this, so the query will result in full table scans. It will also need temporary tables and filesort, i.e. the query is relatively slow.
Without changing the table structure but the format of the data from
1 2 3
to1,2,3
you can at least get rid ofConcat()
andLIKE
by using the slightly faster FIND_IN_SET(str, strlist).Just for completeness and in case you change your mind: http://en.wikipedia.org/wiki/Database_normalization