简体   繁体   English

如何处理并发数据库请求或如何运行简短的SQL查询,同时正在进行长时间运行的查询

[英]How to handle concurrent database request or How to run a short sql query, while a long running query is in progress

Consider there is a user request which needs an sql query to be executed at back-end and resultant data needs to be returned as response, but the user has already initiated another request that consist of a very long running sql query. 考虑有一个用户请求需要在后端执行sql查询,并且结果数据需要作为响应返回,但是用户已经发起了另一个包含很长时间运行的sql查询的请求。

While the longer query is in progress, I need the the database to handle the shorter query (give the shorter query a bit more priority than long running query). 虽然查询正在进行中,但我需要数据库来处理较短的查询(使较短的查询比长时间运行的查询更优先)。

  1. Is it possible to achieve such concurrent execution by tweaking the database server? 是否可以通过调整数据库服务器来实现这种并发执行?
  2. Can Java Threads be used for achieving this? 可以使用Java Threads来实现这一目标吗? Is it a right choice? 这是一个正确的选择吗?

Most databases support concurrent statement execution, usually with Multiversion concurrency control (MVCC) or other mechanism. 大多数数据库支持并发语句执行,通常使用Multiversion并发控制(MVCC)或其他机制。 The client can usually open multiple connections to the database server and run different SQL statemenets in each connection. 客户端通常可以打开与数据库服务器的多个连接,并在每个连接中运行不同的SQL状态。

You need to check how your database server handles concurrency, you can start by taking a look at this answer for MySQL . 您需要检查数据库服务器如何处理并发,您可以先看看MySQL的这个答案 Then look into JDBC Basics docs to understand how to open multiple connections. 然后查看JDBC Basics文档以了解如何打开多个连接。 There will be gotchas eg transaction isolation levels or lock handling that will be specific to your use case. 将会出现特定于您的用例的事务隔离级别或锁定处理等问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM