4

就像我之前解释那样,我的 mysql 数据库服务器出现了一些问题。我会知道您的意见并有一些想法,因为我在一个黑洞中,我不知道,因为正在发生服务器的行为。

我将尝试解释所有环境。我有 1 个数据库,有很多表。我们用 java 制作了一个导出器工具,可以从数据库中导出所有数据。数据存储在 5 个不同的表中,我需要将数据连接到 5 个表中。这些是表格:

DB的结构是一个从一些传感器接收信息并存储它的系统。

测量表:我们从传感器接收到的测量值。

+--------------------+------------+------+-----+---------+----------------+
| Field              | Type       | Null | Key | Default | Extra          |
+--------------------+------------+------+-----+---------+----------------+
| id                 | bigint(20) | NO   | PRI | NULL    | auto_increment |
| version            | bigint(20) | NO   |     | NULL    |                |
| counter            | char(2)    | YES  |     | NULL    |                |
| datemeasurement_id | datetime   | NO   | MUL | NULL    |                |
| datereal_id        | datetime   | NO   | MUL | NULL    |                |
| delayed            | bit(1)     | NO   |     | NULL    |                |
| frequency          | tinyint(4) | YES  |     | NULL    |                |
| measuringentity_id | bigint(20) | NO   | MUL | NULL    |                |
| real               | bit(1)     | NO   |     | NULL    |                |
| tamper             | bit(1)     | NO   |     | NULL    |                |
| value              | float      | NO   |     | NULL    |                |
+--------------------+------------+------+-----+---------+----------------+

测量实体表:一个传感器可以测量多个事物(温度、湿度)。这些就是实体。

+--------------+------------+------+-----+---------+----------------+
| Field        | Type       | Null | Key | Default | Extra          |
+--------------+------------+------+-----+---------+----------------+
| id           | bigint(20) | NO   | PRI | NULL    | auto_increment |
| version      | bigint(20) | NO   |     | NULL    |                |
| household_id | varchar(4) | NO   | MUL | NULL    |                |
| operative    | bit(1)     | NO   |     | NULL    |                |
| type         | char(20)   | NO   |     | NULL    |                |
| unit         | char(3)    | NO   |     | NULL    |                |
| interval     | float      | YES  |     | NULL    |                |
+--------------+------------+------+-----+---------+----------------+

sensor_measuring_entity:一个传感器可以关联多个实体。

+--------------------+------------+------+-----+---------+-------+
| Field              | Type       | Null | Key | Default | Extra |
+--------------------+------------+------+-----+---------+-------+
| sensor_id          | bigint(20) | NO   | PRI | NULL    |       |
| measuringentity_id | bigint(20) | NO   | PRI | NULL    |       |
| version            | bigint(20) | NO   |     | NULL    |       |
+--------------------+------------+------+-----+---------+-------+

传感器表:传感器的信息,与上表中的测量实体相关。

+---------------------+-------------+------+-----+---------+----------------+
| Field               | Type        | Null | Key | Default | Extra          |
+---------------------+-------------+------+-----+---------+----------------+
| id                  | bigint(20)  | NO   | PRI | NULL    | auto_increment |
| version             | bigint(20)  | NO   |     | NULL    |                |
| battery             | bit(1)      | NO   |     | NULL    |                |
| identifier          | char(6)     | NO   |     | NULL    |                |
| installationdate_id | datetime    | NO   | MUL | NULL    |                |
| lastreceiveddate_id | datetime    | YES  | MUL | NULL    |                |
| location_id         | bigint(20)  | NO   | MUL | NULL    |                |
| operative           | bit(1)      | NO   |     | NULL    |                |
| tampererror         | smallint(6) | NO   |     | NULL    |                |
+---------------------+-------------+------+-----+---------+----------------+

位置表:放置传感器的位置。

+------------+------------+------+-----+---------+----------------+
| Field      | Type       | Null | Key | Default | Extra          |
+------------+------------+------+-----+---------+----------------+
| id         | bigint(20) | NO   | PRI | NULL    | auto_increment |
| version    | bigint(20) | NO   |     | NULL    |                |
| height     | tinyint(4) | YES  |     | NULL    |                |
| operative  | bit(1)     | NO   |     | NULL    |                |
| place      | char(15)   | NO   | MUL | NULL    |                |
| room       | char(15)   | NO   |     | NULL    |                |
| typesensor | char(15)   | NO   |     | NULL    |                |
| formaster  | bit(1)     | YES  |     | NULL    |                |
+------------+------------+------+-----+---------+----------------+

导出信息的算法对于像这样跨越数据,尝试导出我们可以在单独的 csv 文件中拥有的所有类型的传感器的单独信息非常重要:

for (int i = 0; i < households.length; i++) {

openConnection();

for (int j = 0; j < values.length; j++) {

    for (int k = 0; k < rooms.length; k++) {

        if (places.length > 0) {

        for (int l = 0; l < places.length; l++) {

            for (int m = 0; m < height.length; m++) {

                export(startDate2, endDate,
                    households[i], values[j],
                    rooms[k], places[l],height[m]);
                }
            }
        } else {
            for (int m = 0; m < height.length; m++) {

                export(startDate2, endDate,
                    households[i], values[j],
                    rooms[k], null, height[m]);
            }
        }

    }
}

try {
    connection.close();
} catch (SQLException e1) {
    e1.printStackTrace();
}

        }


public void export(String startTime, String endTime, String household,
        String type, String room, String place, String height)
        throws ExporterException {


    String sql = buildSQLStatement(startTime, endTime, household, type,
            room, place, height);

    Statement query;

    try {
        query = connection.createStatement();
        ResultSet result = query.executeQuery(sql); 
        …
        (The exporting to csv code)
        …





private String buildSQLStatement(String startTime, String endTime,
        String household, String type, String room, String place,
        String height) {


    String sql = "select HIGH_PRIORITY m.datemeasurement_id, me.type, l.place, m.value, l.room, l.height, s.identifier "
            + "FROM measurement as m STRAIGHT_JOIN measuring_entity as me ON m.measuringentity_id = me.id "
            + "STRAIGHT_JOIN sensor_measuring_entity as sme ON me.id = sme.measuringentity_id "
            + "STRAIGHT_JOIN sensor as s ON sme.sensor_id = s.id "
            + "STRAIGHT_JOIN location as l ON l.id = s.location_id"
            + " WHERE m.datemeasurement_id "
            + " >"
            + "'"
            + startTime
            + "'"
            + " AND m.datemeasurement_id"
            + " <"
            + "'"
            + endTime
            + "'"
            + " AND m.measuringentity_id"
            + " IN (SELECT  me.id FROM measuring_entity AS me WHERE me.household_id="
            + "'"
            + household
            + "'"
            + ")";

我的大问题是:有时这个应用程序使用数据库中的这段代码,运行起来真的很慢。MySQL 运行速度非常慢,而 MYSQL 在其他时候运行速度非常快。我们无法理解为什么会发生这种行为差异。

例如,当它很慢时(CPU 的 0.3-0%),从数据库中导出所有数据可能需要大约 3 天(大约 200.000 个查询),但就像我之前说的,有些时候服务器会执行在 30-40 分钟内完成相同的工作(CPU 的 85%)。

我们看到的问题是,当行为缓慢时,mysql 会花费大量时间处于“准备”状态(每个查询大约 140 秒),试图优化查询,但就像我说的那样,这种情况只会发生几次. 不是每次。

1016 | root   | localhost:53936                | OptimAAL      | Query   |   10 | preparing | select HIGH_PRIORITY m.datemeasurement_id, me.type, l.place, m.value, l.room, l.height, s.identifier

这些是可以执行的查询之一:

EXPLAIN select HIGH_PRIORITY m.datemeasurement_id, me.type,
l.place,m.value, l.room, l.height, s.identifier
FROM measurement as m
STRAIGHT_JOIN measuring_entity as me ON m.measuringentity_id=me.id
STRAIGHT_JOIN sensor_measuring_entity as sme ON me.id=sme.measuringentity_id
STRAIGHT_JOIN sensor as s ON sme.sensor_id=s.id
STRAIGHT_JOIN location as l ON l.id=s.location_id
WHERE m.datemeasurement_id  >'2012-01-19 06:19:00'
AND m.datemeasurement_id <'2012-01-19 06:20:00'
AND m.measuringentity_id IN (SELECT  me.id FROM measuring_entity AS me
WHERE me.household_id='0022')
AND (height = '0')
AND (type = 'Brightness')
AND (place = 'Corner')
AND (room = 'Living room')
ORDER BY datemeasurement_id

这是解释的结果:

+----+--------------------+-------+-----------------+-----------------------------------------------+--------------------+---------+-------------------------------+------+-------------+
| id | select_type        | table | type            | possible_keys                                 | key                | key_len | ref                           | rows | Extra       |
+----+--------------------+-------+-----------------+-----------------------------------------------+--------------------+---------+-------------------------------+------+-------------+
|  1 | PRIMARY            | m     | range           | FK93F2DBBC6292BE2,FK93F2DBBCA61A7F92          | FK93F2DBBC6292BE2  | 8       | NULL                          |    4 | Using where |
|  1 | PRIMARY            | me    | eq_ref          | PRIMARY                                       | PRIMARY            | 8       | OptimAAL.m.measuringentity_id |    1 | Using where |
|  1 | PRIMARY            | sme   | ref             | PRIMARY,FK951FA3ECA61A7F92,FK951FA3ECF9AE4602 | FK951FA3ECA61A7F92 | 8       | OptimAAL.m.measuringentity_id |    1 | Using index |
|  1 | PRIMARY            | s     | eq_ref          | PRIMARY,FKCA0053BA3328FE22                    | PRIMARY            | 8       | OptimAAL.sme.sensor_id        |    1 |             |
|  1 | PRIMARY            | l     | eq_ref          | PRIMARY,place                                 | PRIMARY            | 8       | OptimAAL.s.location_id        |    1 | Using where |
|  2 | DEPENDENT SUBQUERY | me    | unique_subquery | PRIMARY,FK11C7EA07E6EB51F2                    | PRIMARY            | 8       | func                          |    1 | Using where |
+----+--------------------+-------+-----------------+-----------------------------------------------+--------------------+---------+-------------------------------+------+-------------+

显然,如果我们更改日期间隔的值,数据量会增加很多,因为我们的数据库中有大约 100 万个测量值。

我尝试了一切:

更改 mySQL 配置文件 (/etc/my.cnf):

[mysqld]
#bind-address = 141.21.8.197
max_allowed_packet = 128M
sort_buffer_size = 512M
max_connections=500
query_cache_size = 512M
query_cache_limit = 512M
query-cache-type = 2
table_cache = 80
thread_cache_size=8
key_buffer_size = 512M
read_buffer_size=64M
read_rnd_buffer_size=64M
myisam_sort_buffer_size=64M
innodb_flush_log_at_trx_commit=2
innodb_buffer_pool_size=700M
innodb_additional_mem_pool_size=20M
datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#Enable logs
log = /var/log/mysql/mysql-log.log 
log-error = /var/log/mysql/mysql-error.log  
long_query_time = 1
log-slow-queries = /var/log/mysql/mysql-slow.log
[mysqld_safe]
log-error=/var/log/mysql/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
  • 使用不同类型的 JOIN 来强制它避免优化查询。
  • 使用 nice -20 为过程提供最高优先级。
  • 在代码中使用存储过程而不是查询。
  • 关闭与数据库的任何其他连接,只为我拥有数据库,没有任何其他连接。
  • 更改并尝试优化查询。
  • ...

正如你所看到的,我尝试了一切,但我不知道我能做些什么来确保服务器始终保持快速。

这些是服务器的信息:

MySQL version:  5.1.61-log / x86_64 
RAM: 8 GB
OS: CentOS release 6.2 (Final)
CPU: 4 Cores / Xeon E6510  @ 1.73GHz

我真的很感谢你的帮助,

编辑:

我想补充一点,现在对我来说最大的问题是为什么会发生服务器的不同行为。因为我知道可以优化查询,但有时使用此代码可以非常快速地工作。

我现在的噩梦是知道为什么有时工作速度很快,但并非总是如此。现在我正在与 IT 人员核实是否可能是硬件访问、硬盘或类似问题。

看起来这也可能是 SQL 配置的问题,或者可能是它在 MYSQL 中的查询优化器,但我无法发现我的黑洞的解决方案是什么。

非常感谢你的帮助

4

2 回答 2

2

我可以想到一些你可以做的优化。

首先,使用绑定变量和准备好的语句。

PreparedStatment stmt = connection.prepareStatement(
"select HIGH_PRIORITY m.datemeasurement_id, me.type, l.place, m.value, l.room, l.height, s.identifier "
+ "FROM measurement as m STRAIGHT_JOIN measuring_entity as me ON m.measuringentity_id = me.id "
+ "STRAIGHT_JOIN sensor_measuring_entity as sme ON me.id = sme.measuringentity_id "
+ "STRAIGHT_JOIN sensor as s ON sme.sensor_id = s.id "
+ "STRAIGHT_JOIN location as l ON l.id = s.location_id"
+ " WHERE m.datemeasurement_id  > ? "
+ " AND m.datemeasurement_id  < ? "
+ " AND m.measuringentity_id IN (SELECT  me.id FROM measuring_entity AS me WHERE me.household_id= ? )";

stmt.setDate(1, startDate);
stmt.setDate(2, endDate);
stmt.setString(3, household);

stmt.executeQuery();

其次,删除 IN - 你不能在这里对测量实体使用单独的连接吗?IN 通常表现不佳。

第三,您可以使用批处理来执行插入吗?批量插入应该可以显着提高速度。

这些是我能想到的几件事。如果您的查询没有在 Java 端进行优化,那么世界上所有的 SQL 调优都将无济于事。

于 2012-05-04T10:50:39.393 回答
0

我的总体答案是,只对所有数据进行一次查询,然后用 java 将其拆分。(使用几乎相同的代码,您在那里发布)

第一个瓶颈是为每个家庭打开连接。第二个取决于家庭、价值、房间、地点、高度数组的大小。发送这么多查询是个坏主意,因为连接的开销非常大。您应该将每条信息连接到一个(可能是大查询)中并计算您需要多长时间来获取数据)。

于 2012-05-04T10:50:23.237 回答