如何为mysql查询添加注释,以便它们显示在日志中?

时间:2022-11-26 00:49:17

I wanna be able to add a little note, at the beginning of each query, so when I see it in the processlist, or "mytop", I can tell where its running.

我希望能够在每个查询的开头添加一个小注释,所以当我在进程列表或“mytop”中看到它时,我可以告诉它在哪里运行。

Is something like this possible?

这样的事情可能吗?

2 个解决方案

#1


7  

Not sure this would work, but worth trying.

不确定这会有用,但值得一试。

Simply add "/* some comment or tag */ " before whatever SQL query is sent normally.

只需在正常发送任何SQL查询之前添加“/ *一些注释或标记* /”。

It is possible that mySQL server will remove this comment as part of its query analysis/preparation, but it may just leave it as well, so it shows as such in logs and other monitoring tools.

作为查询分析/准备工作的一部分,mySQL服务器可能会删除此注释,但它也可能会将其留下,因此它会在日志和其他监视工具中显示。

In case the comments get stripped away, and assuming SELECT queries, a slight variation on the above would be to add a calculated column as the first thing after SELECT, something like

如果注释被剥离,并假设SELECT查询,上面的一个小变化将是添加计算列作为SELECT后的第一件事,类似于

SELECT IF('some comment/tag' = '', 1, 0) AS BogusMarker, here-start-the-original-select-list
-- or
SELECT 'some [short] comment/tag' AS QueryID, here-start-the-original-select-list

This approach has the drawback of introducing an extra column value, with each of the results row. The latter form, actually use the "comment/tag" value as this value, which may be helpful for debugging purposes.

这种方法的缺点是引入了一个额外的列值,每个结果行。后一种形式,实际上使用“comment / tag”值作为此值,这可能有助于调试目的。

#2


0  

discovered this today:

今天发现了这个:

mysql supports the strange syntax:

mysql支持奇怪的语法:

/*!<min-version> code here */

to embed code that will only be interpreted by mysql, and only mysql of a specified minimum version.
this is documented here: http://dev.mysql.com/doc/refman/5.0/en/comments.html
(used by mysqldump for example.)

嵌入只能由mysql解释的代码,并且只嵌入指定最小版本的mysql。这里有记录:http://dev.mysql.com/doc/refman/5.0/en/comments.html(例如mysqldump使用。)

such comments will not be parsed out, and included in the processlist, unlike normal ones, and that even if the code is not actually executed.

这些注释不会被解析出来,并且包含在进程列表中,与普通的注释不同,即使代码实际上没有被执行。

so you can do something like

所以你可以做点什么

/*!999999 comment goes here */ select foo from bla;

to have a comment that will show up in the processlist, but not alter the code. (until mysql devs decide to bloat their release numbering and release a version numbered over 99.99.99, in which case you will just have to add another digit.

有一个注释将显示在进程列表中,但不会更改代码。 (直到mysql开发人员决定膨胀他们的版本编号并发布编号超过99.99.99的版本,在这种情况下你只需要添加另一个数字。

#1


7  

Not sure this would work, but worth trying.

不确定这会有用,但值得一试。

Simply add "/* some comment or tag */ " before whatever SQL query is sent normally.

只需在正常发送任何SQL查询之前添加“/ *一些注释或标记* /”。

It is possible that mySQL server will remove this comment as part of its query analysis/preparation, but it may just leave it as well, so it shows as such in logs and other monitoring tools.

作为查询分析/准备工作的一部分,mySQL服务器可能会删除此注释,但它也可能会将其留下,因此它会在日志和其他监视工具中显示。

In case the comments get stripped away, and assuming SELECT queries, a slight variation on the above would be to add a calculated column as the first thing after SELECT, something like

如果注释被剥离,并假设SELECT查询,上面的一个小变化将是添加计算列作为SELECT后的第一件事,类似于

SELECT IF('some comment/tag' = '', 1, 0) AS BogusMarker, here-start-the-original-select-list
-- or
SELECT 'some [short] comment/tag' AS QueryID, here-start-the-original-select-list

This approach has the drawback of introducing an extra column value, with each of the results row. The latter form, actually use the "comment/tag" value as this value, which may be helpful for debugging purposes.

这种方法的缺点是引入了一个额外的列值,每个结果行。后一种形式,实际上使用“comment / tag”值作为此值,这可能有助于调试目的。

#2


0  

discovered this today:

今天发现了这个:

mysql supports the strange syntax:

mysql支持奇怪的语法:

/*!<min-version> code here */

to embed code that will only be interpreted by mysql, and only mysql of a specified minimum version.
this is documented here: http://dev.mysql.com/doc/refman/5.0/en/comments.html
(used by mysqldump for example.)

嵌入只能由mysql解释的代码,并且只嵌入指定最小版本的mysql。这里有记录:http://dev.mysql.com/doc/refman/5.0/en/comments.html(例如mysqldump使用。)

such comments will not be parsed out, and included in the processlist, unlike normal ones, and that even if the code is not actually executed.

这些注释不会被解析出来,并且包含在进程列表中,与普通的注释不同,即使代码实际上没有被执行。

so you can do something like

所以你可以做点什么

/*!999999 comment goes here */ select foo from bla;

to have a comment that will show up in the processlist, but not alter the code. (until mysql devs decide to bloat their release numbering and release a version numbered over 99.99.99, in which case you will just have to add another digit.

有一个注释将显示在进程列表中,但不会更改代码。 (直到mysql开发人员决定膨胀他们的版本编号并发布编号超过99.99.99的版本,在这种情况下你只需要添加另一个数字。