简体   繁体   中英

The 'correct' way to search for a string in a MySQL LongText field?

I'm working on Nagios plugin to monitor our rsyslog server, which is back-ended into a MySQL database. The plugin operates by generating a unique string, sending it to syslog, and then querying the MySQL DB for the unique string, hence proving that the service is correctly receiving and storing messages.

The table is, unsurprisingly, big (~3.5 million rows, 2.1GB on disk), and in InnoDB format. The column holding the syslog messages is of type LONGTEXT.

I'm currently sending it queries of the form:

SELECT count(*) FROM allremote WHERE message LIKE '%check_rsyslogdb_1328869942%';

Where in this case check_rsyslogdb_1328869942 is the unique string logged by the plugin.

Is there a more efficient way for me to approach this query?

I looked into FULLTEXT indexes, but they are MyISAM only, and as a policy we avoid MyISAM because, it is, apparently, inferior.

It turns out that since I'm using MySQL 5.* I don't need to use a LONGTEXT, I can use a VARCHAR(2048) instead. This lets me create a normal index, and since I know the exact message, I can leave off the %s as long as I add in an extra trailing space at the end (not sure where that's coming from).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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