mysql 最佳化問題 Using where; Using filesort

來源:互聯網
上載者:User
explain select userid from team_enewsmember where agentid in(select userid from team_enewsmember where agentid='2' and levelid>=1 group by userid) and levelid>=1 group by userid

如果主鍵userid與agentid聯合索引就會出現 Using where; Using index

怎麼都去不掉 Using where; Using filesort,請高手指點。


回複討論(解決方案)

userid 是主鍵,幹嘛還要 group by userid ??
主鍵必須唯一,再 group 就畫蛇添足了

userid 是主鍵,幹嘛還要 group by userid ??
主鍵必須唯一,再 group 就畫蛇添足了

感謝哈!去掉後就只有Using where了

再問下用inner join或left join如何得到上面的查詢再查詢結果。

再查詢結果 是什麼意思?
inner join 和 left join 有不同的結果集,不能用 或 並列起啦

再查詢結果 是什麼意思?
inner join 和 left join 有不同的結果集,不能用 或 並列起啦

mysql子查詢語句改成inner join 或 left join

才發現你查的是同一張表
那自串連不就可以了嗎?

才發現你查的是同一張表
那自串連不就可以了嗎?

是同一個表,請問向這種情況自串連該怎麼寫,麻煩給個範例。自己寫了一個不行,所以就沒用。

大致這樣吧

select userid  from team_enewsmember a, team_enewsmember b  where a.agentid=b.userid and b.agentid='2' and b.levelid>=1 and a.levelid>=1

大致這樣吧

select userid  from team_enewsmember a, team_enewsmember b  where a.agentid=b.userid and b.agentid='2' and b.levelid>=1 and a.levelid>=1


select a.userid from team_enewsmember as a,team_enewsmember as b where a.userid=b.userid and b.agentid='2' and b.levelid>=1
select a.userid 謝謝!這樣可以了,第三級節點以及第四級怎麼查,第三級把自串連作為子查詢嗎?
2的第一個節點3
3的第一個節點4,
4的第一個節點5

你是查級聯資料嗎?那要用遞迴,或修改資料結構
如果只是固定的 4 級以內,這樣也湊合

你是查級聯資料嗎?那要用遞迴,或修改資料結構
如果只是固定的 4 級以內,這樣也湊合

是的,需要根據查出來的userid再查詢每級userid相關的其他資料。不過這樣可以了,謝謝哈!
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.