简体   繁体   English

如何优化此SQL查询?

[英]How to optimize this SQL query?

I have a database with questions and answers that get translated into all languages that site is used by. 我有一个包含问题和答案的数据库,这些问题和答案被翻译成网站使用的所有语言。 But when a question is not translated yet I want to show that question in english language. 但是,当一个问题尚未翻译时,我想用英语显示该问题。 So a gettext-like behavior. 因此,类似gettext的行为。

My current SQL query for this looks like this: 我当前对此的SQL查询如下所示:

SELECT * FROM questions_view WHERE `language` = "de" AND `#parent` IS NULL
UNION 
SELECT * FROM questions_view WHERE `language` = "en" AND `#parent` IS NULL 
    AND id NOT IN (SELECT id 
                   FROM questions_view 
                   WHERE `language` = "de")

But I feel like this is not the optimal way of doing this. 但是我觉得这不是实现此目的的最佳方法。 Any tips? 有小费吗?

This: 这个:

SELECT  qi.*
FROM    (
        SELECT  DISTINCT id
        FROM    questions_view
        ) qd
JOIN    questions_view qi
ON      qi.id = qd.id
        AND qi.language =
        COALESCE(
        (
        SELECT  language
        FROM    questions_view qn
        WHERE   parent IS NULL
                AND language = 'de'
                AND qn.id = qd.id
        ),
        (
        SELECT  language
        FROM    questions_view qn
        WHERE   parent IS NULL
                AND language = 'en'
                AND qn.id = qd.id
        )
        )

or this: 或这个:

SELECT  COALESCE(qde.question_text, qen.question_text)
FROM    (
        SELECT  DISTINCT id
        FROM    questions_view
        ) qd
LEFT JOIN
        questions_view qde
ON      qde.id = qd.id
        AND qde.language = 'de'
LEFT JOIN
        questions_view qen
ON      qen.id = qd.id
        AND qen.language = 'en'

Which if these queries is better depends on you database system and on how many questions in your database are translated. 如果这些查询更好,哪个取决于您的数据库系统以及数据库中翻译了多少个问题。

See this series of articles in my blog for more detail: 有关更多详细信息,请参阅我的博客中的系列文章:

Hm. I can't think of any other solution. 我想不出任何其他解决方案。 You might set the language to null if a question is not yet translated, which would allow you to modify as follows: 如果尚未翻译问题,则可以将language设置为null ,这将允许您进行如下修改:

select * from questions_view where `language` = "de" and `#parent` is null
union 
select * from questions_view where `language` is null and `#parent` is null 

OTOH it might help to first add the translated questions to a temp table and then perform the "does not exist in German"-check as OTOH,这可能有助于先将翻译后的问题添加到临时表中,然后执行“德语中不存在”-检查为

and not exists (select 1 from temp_translated t where t.id = id)

不知道我是否正确,但这还不够

select * from questions_view where language in ('de','en') and #parent is null

Possibly remove 1 trip to the DB by removing the exists and just taking the first available answer, eg 可能通过删除存在项并仅获取第一个可用答案来删除一次前往数据库的旅程,例如

Select Top 1 *
FROM
(
    select 1 as myRank, * from questions_view where `language` = "de" and `#parent` is null
    union 
    select 2 as myRank, * from questions_view where `language` = "en" and `#parent` is null 
) A
Order By myRank asc

I'd go for danish's answer , furthermore, rather than wildcarding the query, only return the column name's you require rather than retrieving them all. 而且,我会寻求丹麦的答案 ,而不是通配符查询,仅返回所需的列名,而不是全部检索它们。

eg 例如

Select col1, col2, col3, col_etc from questions_view where 
language in ('de','en') and #parent is null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM