提高此查询的性能

发布于 2024-10-09 16:26:01 字数 1993 浏览 0 评论 0原文

我有 3 个带有用户登录的表:

sis_login =>管理员 tb_rb_estrutura =>;协调员 tb_usuario =>;我

创建了一个 VIEW 来通过按级别分隔所有这些用户来统一所有用户,如下所示:

create view `login_names` as select `n1`.`cod_login` as `id`, '1' as `level`, `n1`.`nom_user` as `name` from `dados`.`sis_login` `n1`
union all
select `n2`.`id` as `id`, '2' as `level`, `n2`.`nom_funcionario` as `name` from `tb_rb_estrutura` `n2`
union all
select `n3`.`cod_usuario` as `id`, '3' as `level`, `n3`.`dsc_nome` as `name` from `tb_usuario` `n3`;

因此,对于不同的用户最多可以出现三个重复的 id,这就是我按级别分隔的原因。这个 VIEW 只是根据他的 id 和级别返回我的用户名。考虑到它有大约 500,000 名注册用户,该视图加载大约需要 1 秒。太多的时间,但是当我需要返回我网站论坛上的最新帖子时,它变得非常小。

论坛的表返回用户 ID 和级别,然后在此 VIEW 中查找名称。我已经注册了18个论坛。当我运行查询时,每个论坛需要 1 秒 = 18 秒。我的天啊。每次有人进入我的网站时都会加载此页面。

这是我的查询:

select `x`.`forum_id`, `x`.`topic_id`, `l`.`nome`
from (
select `t`.`forum_id`,  `t`.`topic_id`, `t`.`data`, `t`.`user_id`, `t`.`user_level`
from `tb_forum_topics` `t`
union all
select `a`.`forum_id`, `a`.`topic_id`, `a`.`data`, `a`.`user_id`,  `a`.`user_level`
from `tb_forum_answers` `a` ) `x`
left outer join `login_names` `l`
on `l`.`id` = `x`.`user_id` and `l`.`level` = `x`.`user_level`
group by `x`.`forum_id` asc

USING EXPLAIN:

id  select_type table       type    possible_keys   key key_len ref rows    Extra
1   PRIMARY     <derived2>  ALL NULL        NULL    NULL    NULL    6   Using temporary; Using filesort
1   PRIMARY     <derived4>  ALL NULL        NULL    NULL    NULL    530415   
4   DERIVED     n1      ALL NULL        NULL    NULL    NULL    114  
5   UNION       n2      ALL NULL        NULL    NULL    NULL    2    
6   UNION       n3      ALL NULL        NULL    NULL    NULL    530299   

NULL UNION RESULT ALL NULL NULL NULL NULL NULL
2 派生 t ALL NULL NULL NULL NULL 3
3 UNION r ALL NULL NULL NULL NULL 3
NULL UNION RESULT ALL NULL NULL NULL NULL NULL

有人可以帮助我或提出建议吗?

I have 3 tables with user logins:

sis_login => administrators
tb_rb_estrutura => coordinators
tb_usuario => clients

I created a VIEW to unite all these users by separating them by levels, as follows:

create view `login_names` as select `n1`.`cod_login` as `id`, '1' as `level`, `n1`.`nom_user` as `name` from `dados`.`sis_login` `n1`
union all
select `n2`.`id` as `id`, '2' as `level`, `n2`.`nom_funcionario` as `name` from `tb_rb_estrutura` `n2`
union all
select `n3`.`cod_usuario` as `id`, '3' as `level`, `n3`.`dsc_nome` as `name` from `tb_usuario` `n3`;

So, can occur up to three ids repeated for different users, which is why I separated by levels. This VIEW is just to return me user name, according to his id and level. considering it has about 500,000 registered users, this view takes about 1 second to load. too much time, but is becomes very small when I need to return the latest posts on the forum of my website.

The tables of the forums return the user id and level, then look for a name in this VIEW. I have registered 18 forums. When I run the query, it takes one second for each forum = 18 seconds. OMG. This page loads every time somebody enter my website.

This is my query:

select `x`.`forum_id`, `x`.`topic_id`, `l`.`nome`
from (
select `t`.`forum_id`,  `t`.`topic_id`, `t`.`data`, `t`.`user_id`, `t`.`user_level`
from `tb_forum_topics` `t`
union all
select `a`.`forum_id`, `a`.`topic_id`, `a`.`data`, `a`.`user_id`,  `a`.`user_level`
from `tb_forum_answers` `a` ) `x`
left outer join `login_names` `l`
on `l`.`id` = `x`.`user_id` and `l`.`level` = `x`.`user_level`
group by `x`.`forum_id` asc

USING EXPLAIN:

id  select_type table       type    possible_keys   key key_len ref rows    Extra
1   PRIMARY     <derived2>  ALL NULL        NULL    NULL    NULL    6   Using temporary; Using filesort
1   PRIMARY     <derived4>  ALL NULL        NULL    NULL    NULL    530415   
4   DERIVED     n1      ALL NULL        NULL    NULL    NULL    114  
5   UNION       n2      ALL NULL        NULL    NULL    NULL    2    
6   UNION       n3      ALL NULL        NULL    NULL    NULL    530299   

NULL UNION RESULT ALL NULL NULL NULL NULL NULL
2 DERIVED t ALL NULL NULL NULL NULL 3
3 UNION r ALL NULL NULL NULL NULL 3
NULL UNION RESULT ALL NULL NULL NULL NULL NULL

Somebody can help me or give a suggestion?

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

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

发布评论

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

评论(1

悟红尘 2024-10-16 16:26:01

执行您想要的操作:

执行查询 where name = 'whatever'

这将只返回您想要的行。随着用户数量的增加,返回所有行会变得非常慢。而且你已经做了 3 次了。

确保该名称已编入索引,以使其速度非常快。

在调用此函数的函数中,缓存您已在哈希中请求的任何名称。如果没有设置,则进行查询,将结果放入哈希中。如果已设置,则返回该值。

有关如何调用它的更多信息将非常有帮助。

我实际上会推荐不同的表结构:

表 1:用户
用户 ID、名称

表 2:权限
id、UserId、级别

希望这会有所帮助。

To do what you want:

Do a query where name = 'whatever'.

This will return to you just the row that you want. Returning all rows will get very slow very quickly as the number of users increases. And you're doing it 3 times.

Make sure that name is indexed to make it very quick.

In a function that calls this, cache any name you've already requested in a hash. If it is not set, do the query, put the result in the hash. If it is set, return the value.

More information about how this is being called would be very helpful.

I would actually recommend a different table structure:

Table 1: Users
UserId, Name

Table 2: Permissions
id, UserId, Level

Hope this helps.

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