I'm having some truobles in with this query, everytime when i use it the cpu usage goes from a 5% to 67%-100%.
I'm running the mysql server in ubuntu by a java service but even if i execute the query via any mysql ide the results are the same.
I have made some search in the web about it so i'm posting the mysql's config file. I add some atributes then i had found in some post but i think i just made it worse.
Well, this is my my.cnf file:
[mysqld]
innodb_file_per_table=1
innodb_buffer_pool_size = 256M
wait_timeout = 1800
local-infile=0
open_files_limit=10192
query_cache_size=128M
join_buffer_size=128K
thread_cache_size=4
table_cache=64
key_buffer_size=128M
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 1336
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
bind-address = 0.0.0.0
key_buffer = 2014M
max_allowed_packet = 2014M
thread_stack = 512K
thread_cache_size = 1024
myisam-recover = BACKUP
max_connections = 200
query_cache_limit = 2048M
log_error = /var/log/mysql/error.log
expire_logs_days = 10
max_binlog_size = 100M
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[mysql]
[isamchk]
key_buffer = 16M
!includedir /etc/mysql/conf.d/
I'm using this query:
select regPosition.deviceId, count(regPosition.speed), max(regPosition.speed) from regPosition where (TIMESTAMPDIFF(MINUTE, lastPositionTime,now()) <= '5') and regPosition.speed >= '10' group by regPosition.deviceId;
The table's type is Myisam and it has about 2M registry and has idPosition as a index. This is the create table query:
CREATE TABLE `regPosition` (
`idPosition` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Id autoincremental.',
`deviceId` int(5) NOT NULL COMMENT 'Id numérico del equipo. Identificador único para cada vehículo.',
`lastPositionTime` datetime NOT NULL COMMENT 'Fecha/hora en que se registra la marca de posición (realizada por el dvr).',
`divisionew` varchar(2) DEFAULT NULL COMMENT 'Orientación Este u Oeste.',
`longitude` int(11) NOT NULL COMMENT 'longitud.',
`divisionns` varchar(2) DEFAULT NULL COMMENT 'Orientación Norte o Sur.',
`latitude` int(11) NOT NULL COMMENT 'Latitud.',
`direction` int(11) DEFAULT NULL COMMENT 'Dirección en que apunta el dispositivo.',
`gradeLon` varchar(100) DEFAULT NULL COMMENT 'Longitud transformada a grados (en decimal).',
`gradeLat` varchar(100) DEFAULT NULL COMMENT 'Latitud transformada a grados (en decimal).',
`speed` int(11) NOT NULL COMMENT 'Velocidad del vehículo. Registrada por el dvr',
PRIMARY KEY (`idPosition`),
KEY `index` (`idPosition`) USING HASH
) ENGINE=MyISAM AUTO_INCREMENT=6562682 DEFAULT CHARSET=latin1;
[EDIT]
The purpose of the query it's to get the device's id and the number of times than the speed it's bigger than 10 (that's just an example, it could be more) and get the max speed recorded by the database.
The idea of this it's the following: If the speed goes bigger than 60kmh for 5 times in a 5 minutes lapse, i need to know the device's id, max speed and the number of times that exceeded the speed limit.
If you can give me any help i would be very happy :).
Thanks for the help.
WHEREagainst computed values is virtually guaranteed to result in a table scan, and that's bad news if you have a lot of data.lastPositionTimeshould make yourWHEREclause much much faster. Also, if you're doing this in another program then you could keep track of the maximumidPositionpresent in the table each iteration and then addAND idPosition > {previousMaxIdPosition}to your query.