I have 2 tables : galeries (id, name) and pictures (id, name, galery_id which is a FK to galeries).
If I want to select all galeries and, for each, randomly select one of the related pictures, I do this request :
SELECT g.id AS gallery_id,
|
g.nas AS gallery_name,
|
p.id AS picture_id,
|
p.name AS picture_name
|
FROM
|
(
|
SELECT gal.id,
|
gal.name,
|
(
|
SELECT pi.id
|
FROM pictures pi
|
WHERE pi.gallery_id = gal.id
|
ORDER BY RAND()
|
LIMIT 1
|
) AS p_random
|
FROM galleries gal
|
) g
|
LEFT JOIN pictures p
|
ON p.id = g.p_random
|
ORDER BY gallery_name ASC
|
In MySQL, this request works godd : I have a picture randomly selected, and I have all information (like the name). But, in MariaDB, no. RAND() and g.p_random are not equals !
If I modify my request like this :
SELECT g.id AS gallery_id,
|
g.nas AS gallery_name,
|
p.id AS picture_id,
|
p.name AS picture_name,
|
g.p_random AS r1,
|
g.p_random AS r2,
|
g.p_random AS r3
|
FROM
|
(
|
SELECT gal.id,
|
gal.name,
|
(
|
SELECT pi.id
|
FROM pictures pi
|
WHERE pi.gallery_id = gal.id
|
ORDER BY RAND()
|
LIMIT 1
|
) AS p_random
|
FROM galleries gal
|
) g
|
LEFT JOIN pictures p
|
ON p.id = g.p_random
|
ORDER BY gallery_name ASC
|
r1, r2 and r3 have the same value in MySQL, but not in MariaDB.
|
Is it a bug or is it a normal behavior ?
- relates to
-
MDEV-23066
RAND() evaluated and filtered twice with subquery
-
-
Confirmed
{"report":{"fcp":2143.399999856949,"ttfb":880.6999998092651,"pageVisibility":"visible","entityId":27700,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"867e57fd-4bcc-4176-8338-602b7054b77f","navigationType":0,"readyForUser":2249.199999809265,"redirectCount":0,"resourceLoadedEnd":2102.0999999046326,"resourceLoadedStart":888.5999999046326,"resourceTiming":[{"duration":579,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":888.5999999046326,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":888.5999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1467.5999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":579.2999999523163,"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":889,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":889,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1468.2999999523163,"responseStart":0,"secureConnectionStart":0},{"duration":809.5999999046326,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":889.0999999046326,"connectEnd":889.0999999046326,"connectStart":889.0999999046326,"domainLookupEnd":889.0999999046326,"domainLookupStart":889.0999999046326,"fetchStart":889.0999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":1469.8999998569489,"responseEnd":1698.6999998092651,"responseStart":1489.8999998569489,"secureConnectionStart":889.0999999046326},{"duration":976.3999998569489,"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":889.2999999523163,"connectEnd":889.2999999523163,"connectStart":889.2999999523163,"domainLookupEnd":889.2999999523163,"domainLookupStart":889.2999999523163,"fetchStart":889.2999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":1480.2999999523163,"responseEnd":1865.6999998092651,"responseStart":1497.6999998092651,"secureConnectionStart":889.2999999523163},{"duration":616.5999999046326,"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":889.5,"connectEnd":889.5,"connectStart":889.5,"domainLookupEnd":889.5,"domainLookupStart":889.5,"fetchStart":889.5,"redirectEnd":0,"redirectStart":0,"requestStart":1480.5999999046326,"responseEnd":1506.0999999046326,"responseStart":1501.7999999523163,"secureConnectionStart":889.5},{"duration":610.9000000953674,"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":889.6999998092651,"connectEnd":889.6999998092651,"connectStart":889.6999998092651,"domainLookupEnd":889.6999998092651,"domainLookupStart":889.6999998092651,"fetchStart":889.6999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":1480.5999999046326,"responseEnd":1500.5999999046326,"responseStart":1497,"secureConnectionStart":889.6999998092651},{"duration":616.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":889.7999999523163,"connectEnd":889.7999999523163,"connectStart":889.7999999523163,"domainLookupEnd":889.7999999523163,"domainLookupStart":889.7999999523163,"fetchStart":889.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":1480.7999999523163,"responseEnd":1506.2999999523163,"responseStart":1504.1999998092651,"secureConnectionStart":889.7999999523163},{"duration":588.0999999046326,"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":890.0999999046326,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":890.0999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1478.1999998092651,"responseStart":0,"secureConnectionStart":0},{"duration":629.3000001907349,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":890.1999998092651,"connectEnd":890.1999998092651,"connectStart":890.1999998092651,"domainLookupEnd":890.1999998092651,"domainLookupStart":890.1999998092651,"fetchStart":890.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":1500.8999998569489,"responseEnd":1519.5,"responseStart":1517.6999998092651,"secureConnectionStart":890.1999998092651},{"duration":588.2000000476837,"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":890.3999998569489,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":890.3999998569489,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1478.5999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":643.3999998569489,"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":890.5,"connectEnd":890.5,"connectStart":890.5,"domainLookupEnd":890.5,"domainLookupStart":890.5,"fetchStart":890.5,"redirectEnd":0,"redirectStart":0,"requestStart":1501.2999999523163,"responseEnd":1533.8999998569489,"responseStart":1518.2999999523163,"secureConnectionStart":890.5},{"duration":966.5999999046326,"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":895.7999999523163,"connectEnd":895.7999999523163,"connectStart":895.7999999523163,"domainLookupEnd":895.7999999523163,"domainLookupStart":895.7999999523163,"fetchStart":895.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":1788.5999999046326,"responseEnd":1862.3999998569489,"responseStart":1861.2999999523163,"secureConnectionStart":895.7999999523163},{"duration":1206.2999999523163,"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":895.7999999523163,"connectEnd":895.7999999523163,"connectStart":895.7999999523163,"domainLookupEnd":895.7999999523163,"domainLookupStart":895.7999999523163,"fetchStart":895.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":2090.899999856949,"responseEnd":2102.0999999046326,"responseStart":2101.399999856949,"secureConnectionStart":895.7999999523163},{"duration":278.2999999523163,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1848.5999999046326,"connectEnd":1848.5999999046326,"connectStart":1848.5999999046326,"domainLookupEnd":1848.5999999046326,"domainLookupStart":1848.5999999046326,"fetchStart":1848.5999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":2094.699999809265,"responseEnd":2126.899999856949,"responseStart":2126,"secureConnectionStart":1848.5999999046326}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":679,"responseStart":881,"responseEnd":889,"domLoading":886,"domInteractive":2334,"domContentLoadedEventStart":2334,"domContentLoadedEventEnd":2402,"domComplete":3171,"loadEventStart":3171,"loadEventEnd":3172,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":2311.5},{"name":"bigPipe.sidebar-id.end","time":2312.199999809265},{"name":"bigPipe.activity-panel-pipe-id.start","time":2312.399999856949},{"name":"bigPipe.activity-panel-pipe-id.end","time":2313.899999856949},{"name":"activityTabFullyLoaded","time":2458.699999809265}],"measures":[],"correlationId":"7cd1349c340b74","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":121,"dbReadsTimeInMs":11,"dbConnsTimeInMs":21,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
This is a bug if different r1,r2,r3 is a bug in following example:
create table t1 (a int);
insert into t1 values (1),(2);
create view v1 (a,r) as select a,rand() from t1;
select a, r as r1, r as r2, r as r3 from v1;
drop view v1;
drop table t1;