优化传递参数查看

发布于 2024-08-18 16:56:00 字数 474 浏览 5 评论 0原文

我在 mysql 中有相当复杂的视图,比如

select filter.id as filter_id, person.id, person.name
from person, filter
inner  join 
...
left join
...
where person_match_filter_condition ...
group by filter.filter_id, person.id, person.name

查询过滤器对应于域特定条件的人。

视图的典型用途是:

select * from where filter_id = some_value

问题是mysql无法优化查询。它在获取所有过滤器的数据后通过 filter_id 应用配置 - 非常低效。 从其他表获取 filter_id 的想法不适合我的情况。

如何转换查询以使其更有效?

I have quite complicated view in mysql, like

select filter.id as filter_id, person.id, person.name
from person, filter
inner  join 
...
left join
...
where person_match_filter_condition ...
group by filter.filter_id, person.id, person.name

Query filters person which corresponds domain specific conditions.

Typical use of view is:

select * from where filter_id = some_value

Problem is that mysql cannot optimize query. It applies confition by filter_id AFTER get data for all filters - very ineffective.
Idea to get filter_id from other tables is not good for my case.

How can I transform my query to make it more effective?

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

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

发布评论

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

评论(2

不弃不离 2024-08-25 16:56:00

将长查询包装在过程中,并将过滤器作为参数传递给过程调用。然后,您不使用视图来调用该过程,该过程将为您构建整个查询并运行优化的查询。

Wrap the long query in a procedure, and pass the filters to the procedure call as parameters. Then instead of using views you call the procedure, the procedure will build you the entire query and will run optimized query.

甜妞爱困 2024-08-25 16:56:00

更好的是,您可以通过创建一个函数从会话变量中获取值,以简单的方式将参数传递到视图。有关技术,请参阅 https://www.stackoverflow.com/questions/14511760。这是我的创建函数的副本,您可能希望模仿它。
分隔符 //

创建函数 fn_getcase_id()
返回中度(11)
确定性无 SQL
开始

查看 stackoverflow.com/questions/14511760 并阅读所有信息两次或更多次。 wh 04/13/2017

RETURN @sv_case_id;

结束//

分隔符;
您将需要创建一个类似的 FN(每个变量一个)。

Better yet, you can pass parameters to your views in a simple manner by creating a Function to GET your values from Session Variables. See https://www.stackoverflow.com/questions/14511760 for the technique. This is a copy of my create function you may wish to pattern after.
DELIMITER //

CREATE FUNCTION fn_getcase_id()
RETURNS MEDIUMINT(11)
DETERMINISTIC NO SQL
BEGIN

see stackoverflow.com/questions/14511760 and read ALL the info TWICE or MORE. wh 04/13/2017

RETURN @sv_case_id;

END//

DELIMITER ;
You will need to create a similar FN (one for each variable).

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