Good day!
I found problem and strange behavior of update
there is table
CREATE TABLE `schedule_test` (
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
`datetime_processed` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
|
`datetime_next` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
|
`sign` bigint(20) unsigned NOT NULL DEFAULT '0',
|
`lock` tinyint(4) NOT NULL DEFAULT '0',
|
PRIMARY KEY (`id`),
|
KEY `idx_lock_dt_nxt` (`lock`,`datetime_next`)
|
) ENGINE=InnoDB;
|
it's a simple task scheduler and table has near 45k rows
Task selecting doing in this way:
update schedule_test as sa
|
set
|
sa.`lock` = 1,
|
sa.sign = 2123123123,
|
sa.`datetime_processed` = now()
|
where
|
sa.`lock` = 0
|
and sa.`datetime_next` <= now()
|
order by sa.`datetime_next` asc
|
limit 10;
|
problem is that update doesnt use index `idx_lock_dt_nxt` for selecting and mysql does table fullscan (read all 45k).
I look in innotop count readed rows.
But if rewrite update to select, it works as should - use undex and innodb read from table only 10 rows.
select sa.`lock`, sign, datetime_next
|
from schedule_test as sa
|
where
|
sa.`lock` = 0
|
and sa.`datetime_next` <= now()
|
order by sa.`datetime_next` asc
|
limit 10;
|
explain of this:
id select_type table type possible_keys key key_len ref rows Extra
|
1 SIMPLE sa ref lock lock 1 const 22716 Using index condition; Using where
|
for me is not understandable nature this behaviour. as i know select and update should use the same execution plan.
As an experiment, i modify index and make it so KEY `idx_lock_dt_nxt` (`datetime_next`)
in this case update begins using it for select rows by date, but i keep in my mind, it works like full scan but little softer, anyway.
because after some time all rows with minimal datetime will be locked (lock=1) and mysql have to scan more and more rows for checking where condition.
i think may be there is an error in choosing good query plan execution for update?
{"report":{"fcp":1325.6999998092651,"ttfb":302.19999980926514,"pageVisibility":"visible","entityId":23901,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"312a6295-b6e9-4bfd-af6b-f3a6b052b72a","navigationType":0,"readyForUser":1422.1999998092651,"redirectCount":0,"resourceLoadedEnd":1275,"resourceLoadedStart":310.5,"resourceTiming":[{"duration":43.39999985694885,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":310.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":310.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":353.89999985694885,"responseStart":0,"secureConnectionStart":0},{"duration":71.79999995231628,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":310.7999999523163,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":310.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":382.59999990463257,"responseStart":0,"secureConnectionStart":0},{"duration":319.7999999523163,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":310.89999985694885,"connectEnd":310.89999985694885,"connectStart":310.89999985694885,"domainLookupEnd":310.89999985694885,"domainLookupStart":310.89999985694885,"fetchStart":310.89999985694885,"redirectEnd":0,"redirectStart":0,"requestStart":384.69999980926514,"responseEnd":630.6999998092651,"responseStart":419.89999985694885,"secureConnectionStart":310.89999985694885},{"duration":407.7000000476837,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true&whisper-enabled=true","startTime":311.09999990463257,"connectEnd":311.09999990463257,"connectStart":311.09999990463257,"domainLookupEnd":311.09999990463257,"domainLookupStart":311.09999990463257,"fetchStart":311.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":385.09999990463257,"responseEnd":718.7999999523163,"responseStart":496.69999980926514,"secureConnectionStart":311.09999990463257},{"duration":107.60000014305115,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":311.39999985694885,"connectEnd":311.39999985694885,"connectStart":311.39999985694885,"domainLookupEnd":311.39999985694885,"domainLookupStart":311.39999985694885,"fetchStart":311.39999985694885,"redirectEnd":0,"redirectStart":0,"requestStart":385.19999980926514,"responseEnd":419,"responseStart":402,"secureConnectionStart":311.39999985694885},{"duration":108.29999995231628,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":311.5,"connectEnd":311.5,"connectStart":311.5,"domainLookupEnd":311.5,"domainLookupStart":311.5,"fetchStart":311.5,"redirectEnd":0,"redirectStart":0,"requestStart":385.39999985694885,"responseEnd":419.7999999523163,"responseStart":419.09999990463257,"secureConnectionStart":311.5},{"duration":178.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":311.59999990463257,"connectEnd":401,"connectStart":401,"domainLookupEnd":401,"domainLookupStart":401,"fetchStart":311.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":447.7999999523163,"responseEnd":490.09999990463257,"responseStart":488.59999990463257,"secureConnectionStart":401},{"duration":72.5,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":311.7999999523163,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":311.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":384.2999999523163,"responseStart":0,"secureConnectionStart":0},{"duration":191.40000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":311.89999985694885,"connectEnd":311.89999985694885,"connectStart":311.89999985694885,"domainLookupEnd":311.89999985694885,"domainLookupStart":311.89999985694885,"fetchStart":311.89999985694885,"redirectEnd":0,"redirectStart":0,"requestStart":486.5,"responseEnd":503.2999999523163,"responseStart":502.59999990463257,"secureConnectionStart":311.89999985694885},{"duration":89.29999995231628,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":312.09999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":312.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":401.39999985694885,"responseStart":0,"secureConnectionStart":0},{"duration":299.2000000476837,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":312.2999999523163,"connectEnd":312.2999999523163,"connectStart":312.2999999523163,"domainLookupEnd":312.2999999523163,"domainLookupStart":312.2999999523163,"fetchStart":312.2999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":487.19999980926514,"responseEnd":611.5,"responseStart":531.1999998092651,"secureConnectionStart":312.2999999523163},{"duration":927.7999999523163,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":333.5,"connectEnd":333.5,"connectStart":333.5,"domainLookupEnd":333.5,"domainLookupStart":333.5,"fetchStart":333.5,"redirectEnd":0,"redirectStart":0,"requestStart":543.2999999523163,"responseEnd":1261.2999999523163,"responseStart":1259.1999998092651,"secureConnectionStart":333.5},{"duration":941.4000000953674,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":333.59999990463257,"connectEnd":333.59999990463257,"connectStart":333.59999990463257,"domainLookupEnd":333.59999990463257,"domainLookupStart":333.59999990463257,"fetchStart":333.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":602.1999998092651,"responseEnd":1275,"responseStart":1263.5999999046326,"secureConnectionStart":333.59999990463257},{"duration":271.59999990463257,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":985.5,"connectEnd":985.5,"connectStart":985.5,"domainLookupEnd":985.5,"domainLookupStart":985.5,"fetchStart":985.5,"redirectEnd":0,"redirectStart":0,"requestStart":1212.3999998569489,"responseEnd":1257.0999999046326,"responseStart":1255.6999998092651,"secureConnectionStart":985.5},{"duration":304.40000009536743,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":1302.6999998092651,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1302.6999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1607.0999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":302.40000009536743,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1326.0999999046326,"connectEnd":1326.0999999046326,"connectStart":1326.0999999046326,"domainLookupEnd":1326.0999999046326,"domainLookupStart":1326.0999999046326,"fetchStart":1326.0999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":1593.6999998092651,"responseEnd":1628.5,"responseStart":1627.8999998569489,"secureConnectionStart":1326.0999999046326}],"fetchStart":0,"domainLookupStart":35,"domainLookupEnd":95,"connectStart":95,"connectEnd":126,"secureConnectionStart":114,"requestStart":127,"responseStart":302,"responseEnd":328,"domLoading":309,"domInteractive":1608,"domContentLoadedEventStart":1608,"domContentLoadedEventEnd":1745,"domComplete":1887,"loadEventStart":1887,"loadEventEnd":1888,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1494.7999999523163},{"name":"bigPipe.sidebar-id.end","time":1495.8999998569489},{"name":"bigPipe.activity-panel-pipe-id.start","time":1496},{"name":"bigPipe.activity-panel-pipe-id.end","time":1497.5999999046326},{"name":"activityTabFullyLoaded","time":1768.1999998092651}],"measures":[],"correlationId":"9b20f322658722","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":96,"dbReadsTimeInMs":11,"dbConnsTimeInMs":19,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Another problem: EXPLAIN shows that rows=
{number of rows scanned by range scan}, not
{number of rows scanned before we find LIMIT}(let's call this "rows problem").