douhuang7263 2012-11-10 20:41 采纳率: 100%
浏览 36
已采纳

MySQL处理来自2个不同连接的查询的顺序是什么?

Let's say I have two files file1.php and file2.php.

file1.php has the following queries:

-query 1
-query 2
-query 3

file2.php has the following queries:

-query 4
-query 5
-query 6

Let's say one visitor runs the first script and another visitor runs the second one exactly at the same time.

My question is: does MySQL receive one connection and keep the second connection in queue while executing all queries of the first script, and then moves on to the second connection?

Will the order of queries processed by MySQL be 1,2,3,4,5,6 (or 4,5,6,1,2,3) or can it be in any order?

What can I do to make sure MySQL executes all queries of one connection before moving on to another connection?

I'm concerned with data integrity. For example, account balance by two users who share the same account. They might see the same value, but if they both send queries at the same time, this could lead to some unexpected outcome

  • 写回答

4条回答 默认 最新

  • dongnuochen9449 2012-11-10 20:59
    关注

    The database can accept queries from multiple connections in parallel. It can execute the queries in arbitrary order, even at the same time. The isolation level defines how much the parallel execution may affect the results:

    If you don't use transactions, the queries can be executed in parallel, and the strongest isolation level still guarantees only that the queries will return the same result as if they were not executed in parallel, but can still be run in any order (as long as they're sorted within each connection)

    If you use transactions, the database can guarantee more:

    The strongest isolation level is serializable, which means the results will be as if no two transactions ran in parallel, but the performance will suffer.

    The weakest isolation level is the same as not using transactions at all; anything could happen.

    If you want to ensure data consistency, use transactions:

    START TRANSACTION;
    ...
    COMMIT;
    

    The default isolation level is read-commited, which is roughly equivalent to serializable with ordinary SELECTs happening out-of-transactions. If you use SELECT FOR UPDATE for every SELECT within the transaction, you get serializable

    See: http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html#isolevel_repeatable-read

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析