- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
EnableCaching
属性在做什么?我在 official documentation 中找不到有关此属性的任何文档.
MySqlCommand command = new MySqlCommand();
command.Connection = connection;
command.CommandType = CommandType.Text;
command.EnableCaching = [true | false];
// other codes...
感谢任何有用的来源、评论和回答。非常感谢。
最佳答案
TLDR:按您认为合适的方式使用它,但它需要对客户端和服务器端进行大量调整才能掌握所需的结果。
摘自 Mysql 手册页 Query Cache Configuration:
Setting the GLOBAL query_cache_type value determines query cache behavior for all clients that connect after the change is made. Individual clients can control cache behavior for their own connection by setting the SESSION query_cache_type value. For example, a client can disable use of the query cache for its own queries like this:
mysql> SET SESSION query_cache_type = OFF;
诚然,以上内容适用于命令行实用程序,但手册对概念的介绍并不知道您使用的是哪种连接器(Java、.NET 等)。这就是它运行的那个。 您的 连接器将 .NET 的 command.EnableCaching
传递到 mysql 连接的 query_cache_type
。这是在逐个连接的基础上进行的,它决定了您是否希望缓存提供您的结果,假设它们甚至被缓存了。这是一个过于宽泛的问题,甚至无法提出。
我尽量缩小这个答案的范围。你应该什么时候使用它?这取决于您的应用程序和您自己。并且取决于您的特定测试尝试是否被缓存,它会因情况而异。
有几件事要看,例如大小设置和是否启用缓存。此外,mysql 守护程序是否具有发送给它的缓存的临时运行时设置,以及在服务器重新启动时这些设置仍然存在的内容。所以这不是一个简单的答案。
为此,我找到了一篇很好的“文章二”,它补充或超越了上述引用文献中的文章。后者确实提供了服务器信息。
第二条是Speed Up Your Web Site With MySQL Query Caching。我将其全部包含在内,因为经过半小时的搜索,它似乎是最好的信息,我希望它不会消失。
One of the best ways to speed up your web application is to enable query caching in your database, which caches commonly used SQL queries in memory for virtually instant access by the next page that makes the same request.
The reason this method is so powerful is that you don’t have to make any changes to your web application, you just have to sacrifice a little bit of memory. This isn’t going to fix all of your problems, but it definitely can’t hurt.
Note: if your application updates tables frequently, then the query cache will be constantly purged and you won’t get much or any benefit from this. This is ideal for an application that mostly does reads against the database, such as a WordPress blog. This also won’t work if you are running on shared hosting.
Enable Caching with Server Running
The first thing you’ll want to do is make sure that your installation of MySQL actually has query caching support available. Most distributions do, but you should check anyway.
You’ll want to run this command from your MySQL console, which will tell you if query caching is available.
mysql> show variables like 'have_query_cache';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| have_query_cache | YES |
+------------------+-------+Don’t mistake this as meaning that query caching is actually enabled, because most hosting providers aren’t going to enable this by default. Oddly enough, my Ubuntu Feisty installation already had it enabled…
Next we’ll need to check and see if query caching is enabled. We’ll need to check more than one variable, so we may as well do it all at once by checking for the variable
query%
mysql> show variables like 'query%';
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 8388608 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
+------------------------------+---------+Here’s the important items in the list and what they mean:
query_cache_size – This is the size of the cache in bytes. Setting this value to 0 will effectively disable caching.
query_cache_type – This value must be ON or 1 for query caching to be enabled by default.
query_cache_limit – This is the maximum size query (in bytes) that will be cached.
If the query_cache_size value is set to 0 or you just want to change it, you’ll need to run the following command, keeping in mind that the value is in bytes. For instance, if you wanted to allocate 8MB to the cache we’d use 1024 * 1024 * 8 = 8388608 as the value.
SET GLOBAL query_cache_size = 8388608;
Similarly, the other options can be set with the same syntax:
SET GLOBAL query_cache_limit = 1048576;
SET GLOBAL query_cache_type = 1;Now how do we tell if it’s actually working? You can use the
SHOW STATUS
command to pull all the variables that start with “Qc” to take a look at what is going on under the hood.mysql> SHOW STATUS LIKE 'Qc%';
+-------------------------+--------+
| Variable_name | Value |
+-------------------------+--------+
| Qcache_free_blocks | 65 |
| Qcache_free_memory | 201440 |
| Qcache_hits | 18868 |
| Qcache_inserts | 2940 |
| Qcache_lowmem_prunes | 665 |
| Qcache_not_cached | 246 |
| Qcache_queries_in_cache | 492 |
| Qcache_total_blocks | 1430 |
+-------------------------+--------+
8 rows in set (0.00 sec)You’ll notice in the stats that I have plenty of free memory left. If your server shows a lot of lowmem prunes, you might need to consider increasing this value, but I wouldn’t spend too much memory on query caching for a web server… you need to leave memory available for apache, php, ruby, or whatever you are using.
Enable in Config File
If you want these changes to survive a reboot or restart of the mysql server, you’ll need to add them into your
/etc/mysql/my.cnf
configuration file for MySQL. Note that it might be in a different location on your installation.Open up the file using a text editor in sudo or root mode, and then add these values if they don’t already exist in the file. If they do exist, just uncomment them.
query_cache_size = 268435456
query_cache_type=1
query_cache_limit=1048576Query caching can significantly improve the speed of your web application, especially if your application does mostly reads. Monitor the status using the methods above and see how it works over time.
关于c# - 什么时候应该使用 MySqlCommand.EnableCaching 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35130467/
我试图验证当 mysql 执行非查询时,它应该返回受影响的行。然而,当我使用存储过程时,它总是返回 0。 注意:此代码是通用的,用于执行任何存储过程并在必要时返回存储过程的输出值。 C#: /
我正在构建一个函数来处理命令,但 MySQLCommand 没有将我的字符串作为以下代码的参数,您能帮助我吗? int executeCommand(const std::string & mySql
我在 C# 中遇到 MySqlParamters 问题。我将在数据库中创建一个新条目,但 MySqlCommand 只写入空值。 MySqlCommand see = new MySqlCom
我有一个查询在 MySql 工作台中运行良好,但产生语法错误 “您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,以了解在附近使用的正确语法':= 0+1 AS Rank,
我正在制作一个应与 MySql 数据库一起使用的应用程序。我收到此错误,但我不知道为什么: Object reference not set to an instance of an object 这
下面的代码似乎没有将参数值插入到查询中(查询没有返回任何内容)。如果我在数据库中测试该查询(当然我输入传递的值而不是 ?author 参数并返回一些行。为什么? var conn = new M
我正在使用 MySqlCommand 在我的 vb.net 应用程序中执行查询插入,现在我有这个查询: UPDATE primo_appointments SET book_datetime =
我正在尝试运行带有参数的 MySqlCommand,但是当我查看 SQL 日志时,该参数出现了。 我尝试过使用 @ 和 ?,也尝试过在我的 sqlconnection 字符串中使用“old synta
我正在创建一种通过传递搜索字段从任何表中选择 ID 的方法。 private int SelectId(string tabela, string campo, string valor) {
在以下代码中,用于获取特定行中的产品列表,该命令仅在我将 productLine 硬编码(连接)到 SQL 中时才返回结果。参数替换永远不会发生。 + "lineName =
我一直在尝试不同的方法来让我的代码正常工作。 我对 C# 和 DB 都很陌生。但问题是我正在尝试为我添加乐队名称和成员的音乐事件修复数据库。 我有 2 个表。一个和乐队在一起,一个和乐队成员在一起。我
我试图在 mysql 数据库中执行 INSERT 查询,但除了代码执行停止并且没有插入任何内容之外,什么也没有发生。这是代码(连接是在另一点建立的并且正在工作): query
我不知道如何使用 Connector/NET 3.6.5 提供的 MySQLCommand。基本上这里是我正在使用的代码和我在运行 ExecuteNonQuery 时得到的异常。我在这里做错了什么?我
代码如下: MySqlCommand cmd = new MySqlCommand( "SELECT * FROM DB_name_here WHERE some_field =@some_v
当我运行以下代码时: query = "select count(*) from table where name = '?name'"; MySqlConnection connection =
在我的一个项目中,我必须从数据库中获取最后插入的 ID,这样我就可以将新插入的数据与内存中的数据同步,而无需执行 SELECT * FROM my_db; 查询,即我我正在执行 SELECT * FR
我有一个 MySql 数据库,里面有九个表。我需要从 C# 中的异步作业动态上传到其中六个。但是为了加快上传速度,其中四个使用MySqlBulkLoader,另外两个使用手动上传的方式。为了支持并发并
我正在尝试使用以下代码插入到 mysql 表中: // open connection here this.conn.Open(); // fields from post request var e
我有一个存储过程,如果没有错误,它可以返回 2 组结果。 第一组只是一个普通的select。 第二组包含错误代码和错误信息,这一组总是返回一行。由于没有错误,错误代码将为 0,错误消息将为空。 和 如
我发现在我的每个查询(需要自己的命令或写入 parameters.clear())上编写 using 语句非常烦人,有时需要在 using block 之外声明变量。与没有处理对象的版本相比,它非常烦
我是一名优秀的程序员,十分优秀!