简体   繁体   English

使用不同的参数多次运行同一查询

[英]Running the same query multiple times with different parameters

I need to run a select SQL query from a .net service against a MYSQL DB. 我需要针对MySQL数据库从.net服务运行选择SQL查询。 The query takes around 1 second to complete and needs to be executed 36 times consecutively with a different date for each run. 该查询大约需要1秒钟才能完成,需要连续执行36次,每次运行的日期不同。

Simple example for the query, where date will change for each execution: 该查询的简单示例,其中每次执行都会更改日期:

SELECT * FROM person where date < "some date"

I would like to know what are my options for running the query, and what is my best option performance wise. 我想知道我运行查询的选项是什么,明智的选择是什么。 Should i run it 36 times against the DB? 我应该对数据库运行36次吗? Use a stored procedure and loop through the different dates? 使用存储过程并循环浏览不同的日期? Any other option? 还有其他选择吗?

Please note that there is no option to change the query to allow fewer executions, I must run it 36 times and i am trying to find out what are the viable options, pros and cons, for each option. 请注意,没有选项可以更改查询以减少执行次数,我必须运行36次,并且我试图找出每个选项的可行选项和优点和缺点。

Edit: 编辑:

I will try to make my query example clearer: 我将尝试使查询示例更清晰:

The query is comprised from several select statements, each select statements is making a calculation: either summing an amount or counting occurrences etc.. Each query is dependent on a date passed to the query. 该查询由几个选择语句组成,每个选择语句都在进行计算:求和或计数出现次数等。每个查询都取决于传递给查询的日期。 I need the results of those calculations for 36 different time periods. 我需要36个不同时间段内这些计算的结果。

The below example is not the original query but only a part of it with some changes for the names of the tables etc.. just to demonstrate the general idea. 下面的示例不是原始查询,而是其中的一部分,只是对表名称等进行了一些更改。

I am currently running the query 36 times from my .Net server against my MYSQL DB. 我目前正在对我的MySQL数据库从.Net服务器运行查询36次。 It just feels like this is not the best way to do this. 只是感觉这不是执行此操作的最佳方法。 I can consider moving the query to a stored procedures and perhaps running the same query in a loop 36 times instead of calling the DB for each query. 我可以考虑将查询移至存储过程,并可能在循环中运行36次相同的查询,而不是为每个查询调用DB。 I wanted to know if anyone has a better idea to tackle the issue of running the same query, with different parameters, many times. 我想知道是否有人有更好的主意来解决多次运行具有不同参数的相同查询的问题。

Example: 例:

SET @id = 11111;
SET @calculations_date = "2019-05-05";
SET @calculations_date_minus_1_year = DATE_SUB(@calculations_date, INTERVAL 1 YEAR);

SELECT customers.id,
IFNULL( (SELECT COUNT(DISTINCT id) FROM customer_data WHERE id = @id AND customer_data.date >= DATE_SUB(@calculations_date, INTERVAL 2 YEAR) AND customer_data.date <= @calculations_date) , 0) as customers_in_last_24_months,
IFNULL( (SELECT SUM(amount) FROM other_customer_data WHERE id = @id AND date <= @calculations_date_minus_1_year), 0) AS total_other_customer_data_until_12_months_before_date,
IFNULL( (SELECT SUM(amount) FROM other_customer_data2 WHERE id = @id AND date <= @calculations_date_minus_1_year), 0) AS total_other_customer_data2_until_12_months_before_date,
IFNULL( (SELECT SUM(amount) FROM other_customer_data3 WHERE id = @id AND date <= @calculations_date_minus_1_year), 0) AS total_other_customer_data3_until_12_months_before_date,
FROM customers
WHERE customers.id = @id;

Thanks! 谢谢!

Well, the first knee-jerk reaction to improving select-statement performance is to introduce an index to the table (in your case to the date column). 好吧,提高选择语句性能的第一个下意识的反应是在表中引入一个索引(在您的情况下为date列)。 Pro: quick, easy Con: more disk space required (depending on the kind of index and table size this can be considerable) 优点:快速,简便缺点:需要更多的磁盘空间(取决于索引的类型和表的大小,这可能会很大)

Another option that comes to my mind is to load the entire table into memory and do the filtering there. 我想到的另一个选择是将整个表加载到内存中并在其中进行过滤。 That is certainly faster, but especially for larger tables often not viable, since you might run out of RAM. 这当然更快,但是特别是对于通常不可行的大表,因为您可能会用完RAM。

If there is any way to rewrite the query, you might want to select for all 36 people at once, but you stated, that "there is no option to change the query"... so I guess that option is out the window? 如果有任何方法可以重写查询,则可能要一次为所有36个人选择,但是您说“没有选项可以更改查询” ...所以我想该选项不在窗外了吗?

You may also experiment around with a materialized view, but I know too little about MySql to make any judgement on whether that makes much sense in your case. 您也可以尝试使用实例化视图,但是我对MySql知之甚少,因此无法判断在您的情况下是否有意义。

Hope I was able to provide you with some options that you can uses as a starting point ;) 希望我能够为您提供一些可以作为起点的选择;)

If there are a million rows before "some date", then it will take a long time to run, and your client will choke on the amount of data. 如果“某个日期”之前有一百万行,那么将需要很长时间才能运行,并且您的客户端会阻塞大量的数据。 So, I claim that this is not a realistic query. 因此,我声称这不是现实的查询。

On the other hand, 另一方面,

SELECT * FROM person where date < "some date"
    ORDER BY date  LIMIT 10

returns only (at most) 10 rows. 仅返回(最多)10行。 If there is INDEX(date) , then the performance of the query will be very fast and nearly constant. 如果存在INDEX(date) ,那么查询的性能将非常快并且几乎恒定。 I would expect milliseconds, not 1 second. 我希望毫秒, 而不是 1秒。

At that speed, you can have 360 users making the query "at the same time". 以这种速度,您可以让360个用户“同时”进行查询。

Or did you mean that one connection is doing 36 SELECTs . 还是您的意思是一个连接正在执行36个SELECTs In this case it seems that they will be getting overlapping information?? 在这种情况下,似乎他们将获得重叠的信息?

The overhead is a significant of simple queries. 对于简单查询而言,开销非常大。 Perhaps we can put the 36 queries into one? 也许我们可以将36个查询合而为一?

Also, do you need all the columns from the table? 另外,您是否需要表中的所有列? Leave out unnecessary columns to cut back on the bulkiness of the transmission. 省略不必要的列以减少传输的体积。

我决定在服务器端动态创建查询,并在每个部分之间包括UNION ALL以避免多次访问数据库。

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

相关问题 在 C# 中使用不同的参数多次运行相同的方法 - Running the same method multiple times with different parameters in c# 在功能文件中多次使用不同的参数运行所有方案? - Running all scenarios in a feature file, multiple times with different parameters? 如果我在不同时间使用相同Linq查询的不同字段,Entity Framework是否会多次查询数据库? - Does Entity Framework query the database multiple times if I use different fields of the same Linq query at different times? 多次使用不同参数的方法 - Use a method multiple times with different parameters efficiently 多次实现包含具有不同类型参数属性的同一个通用接口 - Implement multiple times the same generic interface that includes properties with different type parameters 无限并行运行多次相同的ActionBlock - Running the same ActionBlock multiple times with Unbounded Parallelism 如何避免多次运行同一循环? - How to avoid running the same loop multiple times? BenchmarkDotNet 多次运行相同的基准测试 - BenchmarkDotNet running same benchmark multiple times 查询运行多次并且奇怪地制作 - Query running multiple times and is weirdly made 使用不同的配置多次注册同一对象 - Register same object multiple times with different configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM