简体   繁体   English

如何让这个查询运行得更快?

[英]How to make this query run faster?

I have query like this: 我有这样的查询:

    SELECT 
    `om_chapter`.`manganame` as `link`,
    (SELECT `manganame` FROM `om_manga` WHERE `Active` = '1' AND `om_manga`.`link` = `om_chapter`.`manganame` LIMIT 0,1) AS `manganame`,
    (SELECT `cover` FROM `om_manga` WHERE `Active` = '1' AND `om_manga`.`link` = `om_chapter`.`manganame` LIMIT 0,1) AS `cover`,
    (SELECT `othername` FROM `om_manga` WHERE `Active` = '1' AND `om_manga`.`link` = `om_chapter`.`manganame` LIMIT 0,1) AS `othername`
FROM `om_chapter`
WHERE 
    `Active` = '1' AND 
    (SELECT `Active` From `om_manga` WHERE `om_manga`.`link` = `om_chapter`.`manganame` LIMIT 0,1) AND 
    `id` IN ( SELECT MAX(`id`) FROM `om_chapter` WHERE `Active` = '1' GROUP BY `manganame` )
ORDER BY `id` DESC LIMIT 10

So how can I make this query faster? 那么如何才能更快地进行此查询?

Here are my tables: 这是我的表格:

om_chapter: om_chapter:

id  | manganame     | chapter   | Active
-----------------------------------------
1   | naruto        | 1         | 1
2   | naruto        | 12        | 1
3   | naruto        | 22        | 1
4   | bleach        | 10        | 1
5   | bleach        | 15        | 1
6   | gents         | 1         | 1
7   | naruto        | 21        | 1

om_manga: om_manga:

id  | othername | manganame     | cover     | Active
-----------------------------------------------------
1   | naruto    | naruto        | n.jpg     | 1
2   | bleach    | bleach        | b.jpg     | 1
4   | gents     | gents         | g.jpg     | 1 

First thing i want form this query is to give me 10 last rows form om_chapter by grouping manganame and ordering by id.. i try to use a simple query by using group or even distinct but none of them give me the right result... 我想要形成这个查询的第一件事就是通过对manganame进行分组并按id排序,从om_chapter给出10个最后一行。我尝试使用一个简单的查询,使用组甚至是不同的但是没有一个给我正确的结果......

In a simple query with group or distinct, the result is like this: 在具有group或distinct的简单查询中,结果如下所示:

id  | manganame     | chapter   | Active
-----------------------------------------
7   | prince        | 21        | 1
5   | gent          | 15        | 1
2   | naruto        | 12        | 1
1   | bleach        | 1         | 1

But i want this result: 但我想要这个结果:

id  | manganame     | chapter   | Active
-----------------------------------------
9   | gents         | 21        | 1
8   | bleach        | 21        | 1
7   | prince        | 21        | 1
6   | naruto        | 1         | 1

So i use this: 所以我用这个:

WHERE 
`Active` = '1' AND 
(SELECT `Active` From `om_manga` WHERE `om_manga`.`link` = `om_chapter`.`manganame` LIMIT 0,1) AND 
`id` IN ( SELECT MAX(`id`) FROM `om_chapter` WHERE `Active` = '1' GROUP BY `manganame` )

And i use sub select in where because i want Active's field in om_manga's table be 1.. 我使用sub select in where因为我希望om_manga表中的Active字段为1 ..

For the reset of sub select, i actually didn`t try join, but i will..! 对于子选择的重置,我实际上没有尝试加入,但我会...!

I might have misunderstood your intentions.. But here's one try: 我可能误解了你的意图..但这是一次尝试:

SELECT c.`manganame` AS `link`
     , m.`manganame`
     , m.`cover`
     , m.`othername`
FROM 
     `om_manga` m 
     INNER JOIN `om_chapter` c 
     ON m.`link` = c.`manganame`
     INNER JOIN 
     ( SELECT `manganame`, MAX(`id`) AS `maxid` 
       FROM `om_chapter` 
       WHERE `Active` = '1' 
       GROUP BY `manganame` ) mx
     ON mx.`maxid` = c.`id`
ORDER BY c.`id` DESC LIMIT 10

I would introduce a foreign key contstrain to the om_chapter table to account for the link from a manga to its corresponding chapters. 我将向om_chapter表引入一个外键contstrain,以说明从漫画到其相应章节的链接。

This is how I would conceptualize the problem. 这就是我将问题概念化的方式。

A manga can have many chapters.  A chapter belongs to one manga. 

Then I would alter the om_chapter table, to include a foreign key for the chapter to link to the manga. 然后我会改变om_chapter表,包括链接到漫画的章节的外键。

ALTER TABLE om_Chapter (
ADD mangaID int references om_Manga (id)
)

And drop the manganame column as it is just redundant now 并删除manganame列,因为它现在是多余的

 ALTER TABLE om_Chapter (
 DROP COLUMN manganame
)

Your tables then could look like this. 那么你的表格看起来就像这样。

om_manga: om_manga:

id  | othername | manganame     | cover     | Active
-----------------------------------------------------
1   | naruto    | naruto        | n.jpg     | 1
2   | bleach    | bleach        | b.jpg     | 1
4   | gents     | gents         | g.jpg     | 1 

om_chapter: om_chapter:

id  | chapter   | Active  | mangaID
-----------------------------------------
1   | 1         | 1       |  1
2   | 12        | 1       |  1
3   | 22        | 1       |  1
4   | 10        | 1       |  2
5   | 15        | 1       |  2
6   | 1         | 1       |  4

Finally you could query the tables like so 最后你可以像这样查询表格

SELECT TOP 10 m.Manganame as link,
  m.Manganame,
  m.cover,
  m.othername,

FROM om_manga as m INNER JOIN
  om_chapter as c ON m.ID = c.mangaID

WHERE m.active = 1 AND c.active = 1
ORDER BY m.ID DESC

Why not a simple join? 为什么不简单加入?

SELECT om_chapter.manganame, cover, othername
FROM om_chapter
JOIN om_manga ON om_chapter.manganame = om_manga=manganame
WHERE om_chapter.Active = 1 AND om_manga.Active = 1

unless I'm misreading your version. 除非我误读你的版本。

Use a left outer join (and lose the sub-queries, and the back-quotes): 使用左外连接(并丢失子查询和后引号):

SELECT c.manganame AS link,
       m.manganame AS manganame,
       m.cover     AS cover,
       m.othername AS `othername
  FROM om_chapter    AS c
  LEFT JOIN om_manga AS m
    ON c.manganame = m.manganame
 WHERE c.Active = '1'
   AND c.id IN (SELECT MAX(o.id)
                  FROM om_chapter AS o
                 WHERE o.active = 1
                 GROUP BY o.manganame)
 ORDER BY c.id DESC LIMIT 10

Were it my query, I'd probably select ' c.id AS id ' too. 如果是我的查询,我可能也选择' c.id AS id '。

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

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