Answer the question
In order to leave comments, you need to log in
mysql slow insert
Actually the problem
# Time: 130809 12:07:11
# [email protected]: u[u] @ localhost []
# <b>Query_time: 2.272070</b> Lock_time: 0.000066 Rows_sent: 0 Rows_examined: 0
SET timestamp=1376010431;
INSERT INTO `archive_hour` (`device_id`, `data_date`, `param0`,`param1`,...,`paramn`) VALUES ('334', '2013-07-26 4:00:00', '0','0',...,'0');
SHOW CREATE TABLE `archive_hour`;
CREATE TABLE `archive_hour` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`device_id` int(11) NOT NULL,
`data_date` datetime NOT NULL,
`param0` int(11) DEFAULT NULL,
`param1` int(11) DEFAULT NULL,
...
`paramn` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `device_id` (`device_id`,`data_date`)
) ENGINE=InnoDB AUTO_INCREMENT=4624416 DEFAULT CHARSET=utf8;
select count(*) from archive_hour;
+----------+
| count(*) |
+----------+
| 4077212 |
+----------+
+-------------------+--------+---------+------------+---------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+ | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment | +-------------------+--------+---------+------------+---------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+ | archive_hour | InnoDB | 10 | Compact | 4147021 | 111 | 463470592 | 0 | 111902720 | 5242880 | 4632149 | 2013-08-09 10:58:53 | NULL | NULL | utf8_general_ci | NULL | | | +-------------------+--------+---------+------------+---------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
+---------------------------------+------------------------+ | Variable_name | Value | +---------------------------------+------------------------+ | innodb_adaptive_flushing | ON | | innodb_adaptive_hash_index | ON | | innodb_additional_mem_pool_size | 8388608 | | innodb_autoextend_increment | 8 | | innodb_autoinc_lock_mode | 1 | | innodb_buffer_pool_instances | 1 | | innodb_buffer_pool_size | 268435456 | | innodb_change_buffering | all | | innodb_checksums | ON | | innodb_commit_concurrency | 0 | | innodb_concurrency_tickets | 500 | | innodb_data_file_path | ibdata1:10M:autoextend | | innodb_data_home_dir | | | innodb_doublewrite | ON | | innodb_fast_shutdown | 1 | | innodb_file_format | Antelope | | innodb_file_format_check | ON | | innodb_file_format_max | Antelope | | innodb_file_per_table | ON | | innodb_flush_log_at_trx_commit | 0 | | innodb_flush_method | O_DIRECT | | innodb_force_load_corrupted | OFF | | innodb_force_recovery | 0 | | innodb_io_capacity | 200 | | innodb_large_prefix | OFF | | innodb_lock_wait_timeout | 50 | | innodb_locks_unsafe_for_binlog | OFF | | innodb_log_buffer_size | 8388608 | | innodb_log_file_size | 33554432 | | innodb_log_files_in_group | 2 | | innodb_log_group_home_dir | ./ | | innodb_max_dirty_pages_pct | 75 | | innodb_max_purge_lag | 0 | | innodb_mirrored_log_groups | 1 | | innodb_old_blocks_pct | 37 | | innodb_old_blocks_time | 0 | | innodb_open_files | 300 | | innodb_print_all_deadlocks | OFF | | innodb_purge_batch_size | 20 | | innodb_purge_threads | 0 | | innodb_random_read_ahead | OFF | | innodb_read_ahead_threshold | 56 | | innodb_read_io_threads | 4 | | innodb_replication_delay | 0 | | innodb_rollback_on_timeout | OFF | | innodb_rollback_segments | 128 | | innodb_spin_wait_delay | 6 | | innodb_stats_method | nulls_equal | | innodb_stats_on_metadata | ON | | innodb_stats_sample_pages | 8 | | innodb_strict_mode | OFF | | innodb_support_xa | ON | | innodb_sync_spin_loops | 30 | | innodb_table_locks | ON | | innodb_thread_concurrency | 0 | | innodb_thread_sleep_delay | 10000 | | innodb_use_native_aio | ON | | innodb_use_sys_malloc | ON | | innodb_version | 5.5.31 | | innodb_write_io_threads | 4 | +---------------------------------+------------------------+
Answer the question
In order to leave comments, you need to log in
Is InnoDB really needed here? If there is not enough memory for the plate, it will slow down. Again, apparently this is a VDS, but here the disk is already shared, so other VDS on this server give a good brake.
Look at what innodb_flush_log_at_trx_commit is equal to and drop the result here show variables like 'innodb_%'
If innodb_flush_log_at_trx_commit = 1 then try changing it to 2 (log flush will occur once a second, instead of every transaction)
Colleague, there is a quick solution - start another table, short_log, insert data into it, and in the background - apart from what you need to do quickly - pour it all into a large one. This will be a batch insert and is faster than a single record insert because the index - and we know that the index takes a long time to rebuild - will rebuild once. And now you have this done on insertion of each row. \
You write a separate procedure, run it every 10 minutes - if there are more than, say, 10 thousand lines in short_log - fill everything from it into a large log.
Further. Your int fields allow negative values - I would do UNSIGNED, it is unlikely that your device can have a negative id
Further. Date is essentially the same int, right? Let's store it as an int and store it - and insert it as an int - to save time on conversion when rebuilding the index.
You will need to look at the real date - perform the reverse conversion.
Forgive me if I said a banality, but:
1. partitioning is always good exactly like sharding
2. keeping a whine on vds with 4 million records in the table is at least strange, then use at least MyISAM, although I’m afraid it won’t save either.
3. muscle - 5.5, why didn't you do profiling? maybe it would become clear what everything rests on
4. disk analysis also does not hurt, at least the output of iostat -dx 5 at the time of recording and without it
It seems to me that you are trying to insert a string value into an integer field, the sequel converts the string to an integer and wastes time on this, try removing the quotes from the inserted values, but I could be wrong.
Example:
INSERT INTO `archive_hour` (`device_id`, `data_date`, `param0`,`param1`,...,`paramn`) VALUES (334, '2013-07-26 4:00:00', 0 ,0,...,0);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question