MySQL 中的视图或子查询

发布于 2024-11-08 12:59:55 字数 461 浏览 0 评论 0原文

用户(用户名,Catogory_IDSub_Category_ID

类别Catogory_ID< /code>,Categories_Name)

SubCatogoies(Sub_Category_ID,SubCatogoies_Name)

code 是 FK 及其 PK

问题 1

我需要的是使用所有这三个表创建一个视图OR子查询来获取类别和Sub_Catogory名称作为其ID的

问题2

在exec中什么会更快?

Users(username,Catogory_ID,Sub_Catergory_ID)

Categories (Catogory_ID,Categories_Name)

SubCatogoies(Sub_Catergory_ID,SubCatogoies_Name)

In code are FK's and its PK's

Question 1

What i required is to create a view using all these three tables OR a sub query to get the Catogory and Sub_Catogory names as its ID's

Question 2

In exec what will be more faster ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涙—继续流 2024-11-15 12:59:55

将所有 3 个表中的数据汇总在一起的单个查询看起来像这样:

select u.*, c.Categories_Name, s.Sub_Categories_Name
    from Users as u
    left outer join Categories as c
        on c.Category_ID = u.Category_ID
    left outer join SubCategories as s
        on s.Sub_Category_ID = u.Sub_Category_ID;

这可以制作成视图或简单地按原样运行。至于性能,物化视图可能比每次执行时执行连接的即席查询执行得更快。但除非您发现性能问题,否则我不会尝试按照这些方式进行优化。如果即席查询足够快,则不必担心优化它。

A single query that pulls together data from all 3 tables would look something like this:

select u.*, c.Categories_Name, s.Sub_Categories_Name
    from Users as u
    left outer join Categories as c
        on c.Category_ID = u.Category_ID
    left outer join SubCategories as s
        on s.Sub_Category_ID = u.Sub_Category_ID;

This could be made into a view or simply run as is. As for performance, a materialized view might perform faster than the ad hoc query performing the joins upon each execution. But I wouldn't try to optimize along those lines unless you're seeing a performance problem. If the ad hoc query is fast enough, don't worry about optimizing it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文