- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
就像我解释的那样before ,我的 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 | |
+--------------------+------------+------+-----+---------+----------------+
measuring_entity 表:一个传感器可以测量不止一件事(温度、湿度)。这些就是实体。
+--------------+------------+------+-----+---------+----------------+
| 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 分钟内完成相同的工作(85% 的 CPU)。
我们看到的问题是,当行为缓慢时,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
如您所见,我尝试了所有方法,但我不知道该怎么做才能确保服务器始终保持快速。
这些是服务器的信息:
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 中的查询优化器的问题,但我无法发现我的黑洞的解决方案是什么。
非常感谢您的帮助
最佳答案
我能想到您可以做的一些优化。
首先,使用绑定(bind)变量和准备好的语句。
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 - 您不能在此处针对 measuring_entity 使用单独的联接吗? IN 通常表现不佳。
第三,你能使用批处理来执行你的插入吗?批量插入应该会显着提高速度。
这些是我能即兴想到的几件事。如果您的查询未在 Java 端进行优化,那么世界上所有的 SQL 调优都无济于事。
关于java - MySQL 在 Preparing Statement 中以非常奇怪的行为被阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10447009/
我刚开始学习JSP技术,遇到了瓶颈。 如何从 JSP 声明 block ? 这不起作用: ... 服务器说没有“out”。 U: 我确实知道如何使用返回字符串的方法重写代码,但是有没有办法在 ?
在一个字段中,我想设置一个具有自定义过滤器的自定义分析器-着眼于词干-因此,“闪存卡”和“闪存卡”的词根相同,因此返回的结果相同 当我运行以下查询时,我的命中率很高,但是“闪存卡”和“闪存卡”各自返回
快速提问。 我有一个通过 PInvoke 使用 native DLL 的应用程序,这个 DLL 可能会调用 PostQuitMessage()。 如何避免? (因为我的应用程序不应该关闭) 我试过 A
一些给定的 HTML 文章,例如: Content 与一些基本的 Jquery 结合使用,例如: $(".some_
我正在构建一个灯箱相册。当第一个图像加载时,CSS 转换起作用。当加载后的每个图像都没有。任何想法为什么?加载第一张之后的照片,但没有过渡。 Image.prototype.load = functi
这个问题在这里已经有了答案: Disable recent tasks button on Android 5.0 (2 个答案) 关闭 2 年前。 我知道这个问题之前在这里被问过 Android
我是 Objective-C 的新手,我只是想弄清楚我是否可以使用 block 或选择器作为 UIAlertView 的 UIAlertViewDelegate 参数 - 哪个更合适? 我已经尝试了以
我是 Linux (UNIX) 套接字下套接字编程的新手。我在 Internet 上找到了以下代码,用于为每个连接生成一个线程的 tcp 服务器。但是它不起作用。accept() 函数立即返回,不等待
recv()库函数手册页提到: It returns the number of bytes received. It normally returns any data available, up
我有一个用于其他项目的共享 ts 库。在这个库中有被同一个库的其他资源使用的资源。该库的结构分为 components/*、interfaces/*、services/* 等目录。在每个目录的根目录中
我想在同一行中一个接一个地显示我的 ListView ,但 ListView 显示每个新行中的每个项目。我怎样才能防止换行显示。以便它显示为段落 ListView.builder( shr
我有一个包含数千行的表格。 import React from "react" import { useSelector } from "react-redux"; import { useEffec
假设我通常希望收到关于代码中不完整模式的警告,但有时我知道某个函数的模式不完整,我知道这很好。 是still true GHC 的警告粒度是每个模块的,并且没有办法更改有关特定功能或定义的警告? 最佳
我的网络应用程序发送浏览器通知,我知道如何检查通知的浏览器权限,以及如果未授予权限,如何请求权限。 但是,即使用户授予我的站点发送通知的权限,她可能仍然无法收到通知,因为它们 might be dis
我有 Xcode 3.2.1,并且喜欢使用它,但是当我编辑文本中带有超链接的文件时(例如,带有引用的注释:# see http://example.com)Xcode 将文本变成可点击的超链接。尝试编
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为
我有一个在 MY_Controller 中运行的 acl。如果权限被拒绝,那么此刻,我只是执行 redirect('denied') - 这是一个非常基本的 Controller ,它加载一个非常基本
我一直很好奇尝试从 Chrome 切换到 Firefox Quantum,但是对于 Web 开发遇到了一个我无法轻松解决的主要障碍——它正在缓存我的本地主机文件,因此当我尝试在本地主机加载各种 emb
这真的让我很兴奋!在任何时候,我都会参与多个项目。当我退出Xcode时,下次打开Xcode时,我前一天的所有项目都会自动一一打开。 经常我最终编辑错误的文件,AHHHHHHHHHHH!我可以阻止这种行
我的Wiki上有500个左右的Spambot和大约5个实际注册用户。我已经使用nuke删除了他们的页面,但是他们一直在重新发布。我已经使用reCaptcha控制了spambot的注册。现在,我只需要一
我是一名优秀的程序员,十分优秀!