Files
artcraft/_sql/analytics/generic_inference/jobs_by_ip_address.sql
T

80 lines
2.2 KiB
SQL

-- noinspection SqlDialectInspectionForFile
-- noinspection SqlNoDataSourceInspectionForFile
-- noinspection SqlResolveForFile
-- Test table for write contention
-- NB: Nevermind, there's no index on `created_at`
-- select count(*) from generic_inference_jobs where created_at > now() - interval 1 minute;
-- When the database is under contention, use this form to reduce the number of rows scanned
SELECT
maybe_creator_user_token,
users.username as maybe_creator_username,
creator_ip_address,
count(*) as attempts
FROM (
SELECT
maybe_creator_user_token,
creator_ip_address
FROM (
SELECT maybe_creator_user_token,
creator_ip_address,
created_at
FROM generic_inference_jobs
ORDER BY id DESC
LIMIT 10000
) as j
WHERE j.created_at > NOW() - INTERVAL 30 MINUTE
) as jobs
LEFT OUTER JOIN users
ON users.token = jobs.maybe_creator_user_token
group by maybe_creator_user_token, maybe_creator_username, creator_ip_address
order by attempts desc;
-- When the database is under contention, use this form to reduce the number of rows scanned
SELECT
creator_ip_address,
count(*) as attempts
FROM (
SELECT
maybe_creator_user_token,
creator_ip_address
FROM (
SELECT maybe_creator_user_token,
creator_ip_address,
created_at
FROM generic_inference_jobs
ORDER BY id DESC
LIMIT 10000
) as j
WHERE j.created_at > NOW() - INTERVAL 30 MINUTE
) as jobs
group by creator_ip_address
order by attempts desc;
-- When the database is under contention, use this form to reduce the number of rows scanned
SELECT
maybe_creator_user_token,
users.username as maybe_creator_username,
count(*) as attempts
FROM (
SELECT
maybe_creator_user_token,
creator_ip_address
FROM (
SELECT maybe_creator_user_token,
creator_ip_address,
created_at
FROM generic_inference_jobs
ORDER BY id DESC
LIMIT 10000
) as j
WHERE j.created_at > NOW() - INTERVAL 30 MINUTE
) as jobs
LEFT OUTER JOIN users
ON users.token = jobs.maybe_creator_user_token
group by maybe_creator_user_token, maybe_creator_username
order by attempts desc;