内存排序,指令重新排序和缺乏发生之前的关系(Memory ordering, instruction reordering and lack of happens-before relationship)
轻松订购
用std :: memory_order_relaxed标记的原子操作不是同步操作; 只有线程之间共享每个原子对象的修改顺序。 不同的对象相对于其他线程之间没有排序; 操作可以看不到。
示例 - 轻松订购
#include <atomic> #include <thread> #include <assert.h> std::atomic<int> x{ 0 }; std::atomic<bool> x_is_set{ false }; std::atomic<int> counter{ 0 }; void f1() { x.store(5, std::memory_order_relaxed); // A x_is_set.store(true, std::memory_order_relaxed); // B } void f2() { while (!x_is_set.load(std::memory_order_relaxed)); // C if (x.load(std::memory_order_relaxed) == 5) // D ++counter; // E } int main() { std::thread t1{ f1 }; std::thread t2{ f2 }; t1.join(); t2.join(); assert(counter.load() == 1); // F }
线程t1和线程t2之间没有排序约束。 因此,在A完成商店之前,可以通过t2看到在B中完成的商店; 在这种情况下,main()中的断言F将触发。
这个问题的显而易见的解决方案是使B中的存储具有
std::memory_order_release
并且C中的加载具有std::memory_order_acquire
用于同步目的。 F中的断言似乎永远不会发生。题
但是,由于A和B之间没有发生之前的关系(我错了吗?),编译器/优化器/ CPU不能重新组织函数
f1()
的指令,以便B 在 A 之前排序吗? 这将导致函数f2()
C计算为true
,但D将为false
; 断言可以解雇。是否存在阻止该问题产生的任何事情?
Relaxed ordering
Atomic operations tagged with std::memory_order_relaxed are not synchronization operations; only the modification order of each individual atomic object is shared between threads. Different objects have no ordering between themselves relative to other threads; operations can be seen out of order.
Example – Relaxed ordering
#include <atomic> #include <thread> #include <assert.h> std::atomic<int> x{ 0 }; std::atomic<bool> x_is_set{ false }; std::atomic<int> counter{ 0 }; void f1() { x.store(5, std::memory_order_relaxed); // A x_is_set.store(true, std::memory_order_relaxed); // B } void f2() { while (!x_is_set.load(std::memory_order_relaxed)); // C if (x.load(std::memory_order_relaxed) == 5) // D ++counter; // E } int main() { std::thread t1{ f1 }; std::thread t2{ f2 }; t1.join(); t2.join(); assert(counter.load() == 1); // F }
There are no ordering constraints between thread t1 and thread t2. Therefore, the store done in B can be seen by t2 before the store done by A; the assert F in main() will fire in that case.
The obvious solution to this issue is to make the store in B have
std::memory_order_release
and the load in C havestd::memory_order_acquire
for synchronization purposes. The assert in F would seemingly never fire.Question
However, since there is no happens-before relationship between A and B (am I wrong?), can't the compiler/optimizer/CPU reorganize the instructions in function
f1()
such that B is sequenced-before A? This would cause C in functionf2()
to evaluate totrue
, but D would befalse
; the assert could fire.Is there anything preventing that issue from arising?
原文:https://stackoverflow.com/questions/32574459
最满意答案
Innodb将经常使用的页面存储在内存中,因此它不会在每次查询时都转到磁盘上。 内存缓冲区的大小在配置文件(通常是my.cnf)中定义,选项名称为innodb_buffer_pool_size。 Innodb文件也可以由操作系统缓存。
如果服务器上有大量可用内存,请考虑将innodb_buffer_pool_size增加到适当的值。 更多信息: http : //www.mysqlperformanceblog.com/2007/11/03/choosing-innodb_buffer_pool_size/
插入虚拟记录会使情况变得更糟 - 表格会更大。
Innodb stores frequently used pages in memory so it doesn't go to disk on every query. The size of memory buffer is defined in configuration file (usually my.cnf) and the option name is innodb_buffer_pool_size. Innodb files may also be cached by the operating system.
If you have a lot of memory available on the server consider increasing innodb_buffer_pool_size to appropriate value. More information on this: http://www.mysqlperformanceblog.com/2007/11/03/choosing-innodb_buffer_pool_size/
Inserting dummy records will make it worse - the table will be bigger.
相关问答
更多-
将innodb_buffer_pool_size设置为可用 RAM的70%。 关闭查询缓存。 Set innodb_buffer_pool_size to 70% of available RAM. Turn off the Query cache.
-
InnoDB中索引外键的性能有所改善吗? 答:不会。由于密钥重复,性能会下降。 你不需要 INDEX `job_fk1` (`user`), INDEX `job_fk2` (`job`), 这些将由InnoDB在内部自动创建。 但是您需要拥有users ( id )和jobs ( id )的索引,以便在assignments表上更快地进行操作 http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html “InnoD ...
-
Django缓存失去密钥(Django cache loses keys)[2022-02-15]
在删除locmem , filesystem和database后端的旧值之前,缓存中允许的最大项目数为300.您可以通过设置OPTIONS > MAX_ENTRIES来更改它。 从Django文档 : MAX_ENTRIES :删除旧值之前缓存中允许的最大条目数。 此参数默认为300 。 The maximum number of items allowed in the cache before old values are deleted for locmem, filesystem and datab ... -
您的选择: 询问你的虚拟主机提供商是否可以在他们的服务器上启用InnoDB(有时他们会为你做,或者将你的账户移到支持InnoDB的服务器上)。 获得InnoDB支持的新的虚拟主机提供商。 照顾PHP中的外键依赖关系(例如,您可以使用Zend_Db和Zend_Db_Table来处理数据库,如果您正确设置它们,这些类可以自动处理外键依赖关系)。 将您的应用程序转换为MyISAM表。 就我个人而言,我认为4号是最糟糕的解决方案。 现在有很多支持InnoDB的网站托管服务提供商,所以如果您重新编码大部分应用程序以使 ...
-
MySQL InnoDB查询性能(MySQL InnoDB query performance)[2023-08-16]
我希望你能找到以下链接和我感兴趣的以前的答案。 我希望我能为您提供一个针对您的问题的具体答案,但我没有时间让自己的头脑完全围绕您的模式:命名约定非常糟糕,缺乏有用的信息,不一致性等等...... 但是,您应该在下面找到解决您的问题的解决方案,这与切换到性能较低的myisam引擎无关! 以下答案中的一个片段: 您应该阅读以下内容,了解一下设计良好的innodb表的优点以及如何最好地使用聚簇索引 - 只适用于innodb! 聚集索引: http://dev.mysql.com/doc/refman/5.0/en ... -
更快的InnoDB写道?(faster InnoDB writes?)[2023-07-07]
好的,我找到了这个的原因,希望这将有助于其他人。 ext4在该系统上启用了“障碍”,导致小写入变得非常慢。 这种写入对于数据库文件是正常的。 禁用障碍可提高性能,并在约8分钟内加载相同的SQL转储(速度提高4倍)。 mount -o remount,barrier=0 / 禁用障碍伴随着fs完整性的缺点,所以首先考虑一下...... Ok, I found the reason for this and hopefully this will help others. ext4 had "barriers ... -
在这种情况下,我认为“密钥长度”是指密钥的大小(以字节为单位)。 INT列上的主键需要4个字节。 由于主键始终包含在任何二级索引中,因此最终会得到一个INT + INT键或4 + 4个字符来产生8。 我不完全确定为什么这个号码会引起你的关注。 索引结构本身的开销远远大于表示密钥所需的无关紧要的字节数。 我不确定为什么你会在一个简单的计数上使用强制索引操作。 据我所知,与新版本的Postgres不同,MySQL并没有充分利用这些索引。 我相信这与InnoDB的MVCC如何实现有关。 请记住,事务数据库中表中的 ...
-
InnoDB比MyISAM更好地扩展。 如果您正在谈论数亿行,那么请转到InnoDB并调整搜索引擎。 AFAIK,FULLTEXT在某一点之后变得非常缓慢。 因此,请选择InnoDB +搜索引擎。 InnoDB scales better than MyISAM. If you're talking about hundreds of millions of row then go for InnoDB and adapt a search engine. AFAIK, FULLTEXT becomes r ...
-
MySQL中将多个列的主键作为所选列值的串联进行管理。 因此,第一列可以更快地排序,因为它将在汇总的开始处。 A primary key on multiple colums is managed by MySQL as a concatenation of the chosen columns values. Thus, the first column can be sorted faster, as it will be at the beginning of the aggregate.
-
Innodb将经常使用的页面存储在内存中,因此它不会在每次查询时都转到磁盘上。 内存缓冲区的大小在配置文件(通常是my.cnf)中定义,选项名称为innodb_buffer_pool_size。 Innodb文件也可以由操作系统缓存。 如果服务器上有大量可用内存,请考虑将innodb_buffer_pool_size增加到适当的值。 更多信息: http : //www.mysqlperformanceblog.com/2007/11/03/choosing-innodb_buffer_pool_size/ ...