【发布时间】:2013-06-21 01:41:22
【问题描述】:
我有包含数百万数据的大表(它太大了)。
表格如下
Post
post_id,user_id,description,creation_date, xyz, abc ,etc
primarykey for post :post_id
partition key for Post : creation_date
index on Post : user_id
Comment:
commentid,post_id, comment_creation_date,comment_type,last_modified_date
Primary key of comment = commentid
indexed colums on Comment = commentid, postid
partition key for Comment table = comment_creation_date
注意:我不能以任何方式建立新索引而不改变表架构
评论类型是字符串
现在给定一个comment_type 列表和一个comment_creation_date 范围,我需要找到所有具有该comment_type 类型的帖子。
一个简单的非常低效的解决方案将是
select * from post p, comment c where c.post_id = p.post_id where c.comment_creation_date > ? and c.comment_creation_date < ?
and p.posttype IN (some list)
如何优化这个查询? 如果通过评论的 last_modified_date 而不是comment_date 来做同样的事情怎么办。 注意:
last_modified_date is NOT indexed and comment_date Is
一旦查询成功,我想将一个帖子的所有 cmets 放在一起。 例如 post1 与 c1,c2,c3
PS:我不擅长设计查询。我知道 IN 不利于性能。
【问题讨论】:
-
如果这还不够快,我不确定您是否能够在不以任何方式更改架构的情况下获得更快的速度。模式是性能的一个非常重要的部分。您可以尝试 SELECT * FROM post WHERE EXISTS (SELECT NULL FROM comment WHERE ...) 但我很确定性能会相似。
标签: sql oracle optimization query-optimization