Not equals query is so slow - mysql

I have a query like that:
SELECT * , (
( 1584392725 ) - ( suprayts.time )
) AS timeDiff
FROM (
`suprayts`
)
WHERE `suprayts`.`is_deleted` = '0'
AND `suprayts`.`is_approved` =1
AND `suprayts`.`username` != 'rayben1'
AND `suprayts`.`time` >1584306325
ORDER BY `suprayts`.`is_boosted_by_user` DESC , `suprayts`.`id` ASC
LIMIT 10
This query runs very slow (avg 0.2 seconds), if i delete the following line:
AND `suprayts`.`username` != 'rayben1'
It runs 10x faster. (avg 0.02 secs) How can i speed up this query?
My indexes:
Explain:
My table:
CREATE TABLE IF NOT EXISTS `suprayts` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(15) CHARACTER SET utf8 NOT NULL,
`question` varchar(70) COLLATE utf8mb4_unicode_ci NOT NULL,
`suprayt_photo` varchar(50) CHARACTER SET utf8 NOT NULL,
`time` int(11) NOT NULL,
`endTime` int(11) NOT NULL,
`datetext` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`like_count` int(10) unsigned NOT NULL DEFAULT '0',
`dislike_count` int(10) unsigned NOT NULL DEFAULT '0',
`is_approved` bit(1) NOT NULL DEFAULT b'0',
`is_deleted` enum('1','0') CHARACTER SET utf8 NOT NULL DEFAULT '0',
`is_end_notification_sent` bit(1) NOT NULL DEFAULT b'0',
`open_vote` enum('0','1') CHARACTER SET utf8 NOT NULL DEFAULT '1',
`boost` int(11) NOT NULL DEFAULT '0',
`is_boosted_by_user` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `username_3` (`username`,`suprayt_photo`),
KEY `id` (`id`,`time`,`is_approved`,`is_deleted`),
KEY `username` (`username`,`is_deleted`),
KEY `username_2` (`username`,`datetext`),
KEY `id_2` (`id`,`username`,`time`,`is_approved`,`is_deleted`),
KEY `username_4` (`username`,`time`,`is_approved`,`is_deleted`),
KEY `ix1` (`id`,`time`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=130789 ;

Depending on the SQL pre-compiler if one is being used, or the SQL itself, possible it is sensitive to the order of the query "where" clause not being in DB key order and it is not using the index and instead doing a sequential scan? Just to eliminate the possibility, try putting the WHERE items in the DB index key order.

USE NOT EXISTS
SELECT * , (
( 1584392725 ) - ( suprayts.time )
) AS timeDiff
FROM (
`suprayts`
)
WHERE `suprayts`.`is_deleted` = '0'
AND `suprayts`.`is_approved` =1
AND NOT EXISTS (
SELECT x.no FROM (SELECT 1 AS no) x WHERE `suprayts`.`username` = 'rayben1'
)
AND `suprayts`.`time` >1584306325
ORDER BY `suprayts`.`is_boosted_by_user` DESC , `suprayts`.`id` ASC
LIMIT 10

Related

MYSQL HAVING (not) with ROW_NUMBER() why it returns different row counts

I have simple query:
SELECT
definitions.rowid,
chars,
ROW_NUMBER() OVER (ORDER BY chars, definitions.rowid) AS nr
FROM definitions
INNER JOIN words ON definitions.word = words.rowid
WHERE definitions.valid = True
HAVING chars > 20
on my computer(intel) it returns 36k records
but on client computer(ryzen 2990wx) it returns 99k records (having not working - chars starts form 1)
if i remove join it works ok on both computers
if i remove row_number it works ok on both computers too
if i replace "having" with "and" it works ok on both computers too
on both computers are the same version of mysql (8.018) and the same tables ...
I have a presentation tomorrow - I need any ideas
CREATE TABLE `definitions` (
`rowid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`word` INT(10) UNSIGNED NULL DEFAULT NULL,
`definition` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_polish_ci',
`chars` TINYINT(3) UNSIGNED NULL DEFAULT NULL,
`difficult` TINYINT(3) UNSIGNED NULL DEFAULT '99',
`islocked` BIT(1) NULL DEFAULT b'0',
`valid` BIT(1) NULL DEFAULT b'0',
PRIMARY KEY (`rowid`),
INDEX `word` (`word`)
)
COLLATE='utf8_polish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=100038
;
CREATE TABLE `words` (
`rowid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`word` VARCHAR(64) NULL DEFAULT NULL COLLATE 'utf8_polish_ci',
`display` VARCHAR(64) NULL DEFAULT NULL COLLATE 'utf8_polish_ci',
`difficult` TINYINT(4) UNSIGNED NULL DEFAULT NULL,
`islocked` BIT(1) NULL DEFAULT NULL,
`valid` BIT(1) NULL DEFAULT NULL,
PRIMARY KEY (`rowid`),
INDEX `word` (`word`),
INDEX `display` (`display`)
)
COLLATE='utf8_polish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=57009
;
creation code the same on both computers - created by the same instalator ...
query is a part of table browser/editor with just in time loading (datagridview with virtual mode) so will be very difficult modify its logic by night
HAVING is used with GROUP BY to filter the aggregated data. In your case, you should just add HAVING chars > 20 to the WHERE clause as AND chars > 20

MySql unexpected query result

I have such a tables offers and offer_operating_systems related by offer_id. I made two queris and from my understanding they should return same result, but they don't
Query 1:
select
count(*)
from
`offers`
where
(
select count(*)
from `offer_operating_systems`
where
`offer_operating_systems`.`offer_id` = `offers`.`id`
and
`operating_system` = 'android'
) = 1
order by `id` asc
Query 2:
select
count(*)
from offer_operating_systems
where
operating_system = 'android'
order by `id` asc
Can someone explain to me why the results are not the same? Thanks!
EDITED
operating_systemcolumn is unique so each offer can have only one record with adnroid
Table structures
CREATE TABLE `offers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`type` int(11) NOT NULL DEFAULT '0',
`url` text COLLATE utf8_unicode_ci,
`status` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'pending',
`api_created_at` timestamp NULL DEFAULT NULL,
`api_updated_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `offers_status_index` (`status`)
) ENGINE=InnoDB AUTO_INCREMENT=423 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `offer_operating_systems` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`offer_id` int(10) unsigned NOT NULL,
`operating_system` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `offer_operating_systems_offer_id_operating_system_unique` (`offer_id`,`operating_system`),
KEY `offer_operating_systems_offer_id_foreign` (`offer_id`),
CONSTRAINT `offer_operating_systems_offer_id_foreign` FOREIGN KEY (`offer_id`) REFERENCES `offers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=728 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Slow query when use order by with limit 1

I have a table in mysql with over 80M records. In MYISAM engine.
When I run this query
SELECT id FROM mytable WHERE (key1=-5) AND key2=467476 ORDER BY id DESC LIMIT 1
query is slow and after 5 minutes I must kill query to release table.
But when i just increase limit size this query work successfully in 44ms
For example (I just increase limit size):
SELECT id FROM mytable WHERE (key1=-5) AND key2=467476 ORDER BY id DESC LIMIT 2
Now to solve this issue i try the following query and this work successfully (this is temporary solution)
SELECT id FROM (SELECT id FROM mytable WHERE (key1=-5) AND key2=467476 ORDER BY id DESC LIMIT 2) AS tbl ORDER BY id DESC LIMIT 1
Note
: id is primary and auto increment !
Update:
key1 ==> folder_id
key2 ==> userid
CREATE TABLE `bm60_mails` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL DEFAULT '0',
`betreff` varchar(255) CHARACTER SET utf8 NOT NULL,
`von` varchar(255) CHARACTER SET utf8 NOT NULL,
`an` varchar(255) CHARACTER SET utf8 NOT NULL,
`cc` varchar(255) CHARACTER SET utf8 NOT NULL,
`body` longtext NOT NULL,
`folder` int(11) NOT NULL,
`datum` int(11) NOT NULL DEFAULT '0',
`trashstamp` int(11) NOT NULL,
`priority` enum('low','normal','high') NOT NULL,
`fetched` int(11) NOT NULL DEFAULT '0',
`msg_id` varchar(128) CHARACTER SET utf8 NOT NULL,
`virnam` varchar(128) CHARACTER SET utf8 NOT NULL,
`trained` tinyint(4) NOT NULL DEFAULT '0',
`refs` text CHARACTER SET utf8 NOT NULL,
`flags` int(11) NOT NULL DEFAULT '-1',
`size` int(11) NOT NULL,
`color` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `mailUser` (`userid`),
KEY `mailFlags` (`flags`),
KEY `mailFolder` (`folder`)
) ENGINE=MyISAM AUTO_INCREMENT=95953499 DEFAULT CHARSET=latin1
I suspect if you create a compound index on (key1, key2, id) your problem will vanish.
The id column is also included because that's not implicit in MyISAM tables, althought it is in InnoDB tables.

group_concat() on bit fields returns garbage in Mysql

Table Structure
CREATE TABLE `academicyears` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`campusid` int(11) DEFAULT NULL,
`academicyear` text NOT NULL,
`month` tinyint(4) DEFAULT NULL,
`flag` bit(1) DEFAULT b'1',
PRIMARY KEY (`id`)
);
My Query
SELECT GROUP_CONCAT(ay.`flag`)
FROM academicyears ay
GROUP BY ay.`campusid`
Result
Try this;)
SELECT GROUP_CONCAT(ay.`flag` + 0)
FROM academicyears ay
GROUP BY ay.`campusid`
And check reference here.

Advanced query running slowly

Whenever I am running this query, it takes about 25-30 seconds for it to run. As you can see, the most advanced thing here is to calculate two coalesces within subqueries.
SELECT
g.name,
g.id,
(
SELECT
COALESCE (
SUM(result2 / result1) * (
SUM(IF(result2 != 0, 1, 0)) * 0.1
),
0
) AS res
FROM
gump.war gwr
WHERE
started = 1
AND (UNIX_TIMESTAMP(time) + 7 * 24 * 60 * 60) > UNIX_TIMESTAMP()
AND gwr.guild1 = g.id
AND gwr.winner = g.id
) + (
SELECT
COALESCE (
SUM(result1 / result2) * (
SUM(IF(result1 != 0, 1, 0)) * 0.1
),
0
) AS res1
FROM
gumb.war gwr
WHERE
started = 1
AND (UNIX_TIMESTAMP(time) + 7 * 24 * 60 * 60) > UNIX_TIMESTAMP()
AND gwr.guild2 = g.id
AND gwr.winner = g.id
) AS avg
FROM
gumb.guild g
ORDER BY
avg DESC,
g.point DESC,
g.experience DESC LIMIT 10;
Table structures/schemas:
CREATE TABLE `guild` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(12) NOT NULL DEFAULT '',
`owner` int(10) unsigned NOT NULL DEFAULT '0',
`level` tinyint(2) DEFAULT NULL,
`experience` int(11) DEFAULT NULL,
`win` int(11) NOT NULL DEFAULT '0',
`draw` int(11) NOT NULL DEFAULT '0',
`loss` int(11) NOT NULL DEFAULT '0',
`point` int(11) NOT NULL DEFAULT '0',
`account` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
CREATE TABLE `war` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`guild1` int(10) unsigned NOT NULL DEFAULT '0',
`guild2` int(10) unsigned NOT NULL DEFAULT '0',
`time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`type` tinyint(2) unsigned NOT NULL DEFAULT '0',
`price` int(10) unsigned NOT NULL DEFAULT '0',
`score` int(10) unsigned NOT NULL DEFAULT '0',
`started` tinyint(1) NOT NULL DEFAULT '0',
`winner` int(11) NOT NULL DEFAULT '-1',
`result1` int(11) NOT NULL DEFAULT '0',
`result2` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
Indexes will definitely help, indexing fields used in JOIN criteria and in WHERE clauses carries the most impact.
Generic syntax example:
CREATE INDEX idx_col1col2 ON tbl_Test (Col1, Col2)
You don't likely want to just cram every field used into one index, and you likely shouldn't create an index for each field either.
There are many resources for helping you understand how to build your indexes, here are a couple items:
MySQL CREATE INDEX Syntax
MySQL Index Optimization