Skip to content

Instantly share code, notes, and snippets.

@OllieJones
Created June 23, 2022 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OllieJones/3b9ad08a7191390dac8b952edc613596 to your computer and use it in GitHub Desktop.
Save OllieJones/3b9ad08a7191390dac8b952edc613596 to your computer and use it in GitHub Desktop.
Display a histogram of WordPress option lengths with histogram buckets approximating the log of lengths
-- Display a histogram of WordPress option lengths,
-- with histogram buckets approximating the log of lengths,
WITH lengths AS (
SELECT LENGTH(option_value) l,
autoload,
option_name
FROM wp_options
),
buckets AS (
SELECT autoload, l, option_name,
CASE WHEN l = 0 THEN 0
WHEN l <= 1 THEN 1
WHEN l <= 2 THEN 2
WHEN l <= 5 THEN 5
WHEN l <= 10 THEN 10
WHEN l <= 20 THEN 20
WHEN l <= 50 THEN 50
WHEN l <= 100 THEN 100
WHEN l <= 200 THEN 200
WHEN l <= 500 THEN 500
WHEN l <= 1000 THEN 1000
WHEN l <= 2000 THEN 2000
WHEN l <= 5000 THEN 5000
WHEN l <= 10000 THEN 10000
WHEN l <= 20000 THEN 20000
WHEN l <= 50000 THEN 50000
WHEN l <= 100000 THEN 100000
WHEN l <= 200000 THEN 200000
WHEN l <= 500000 THEN 500000
WHEN l <= 1000000 THEN 1000000
WHEN l <= 2000000 THEN 2000000
WHEN l <= 5000000 THEN 5000000
ELSE 5000001 END bucket
FROM lengths
),
histo AS (
SELECT COUNT(*) num, SUM(l) tot,
bucket,
autoload
FROM buckets
GROUP BY buckets.autoload, buckets.bucket WITH ROLLUP
)
SELECT num "Count", tot "Total Length", COALESCE(bucket,'Total') "Length bucket", COALESCE(autoload,'Total') Autoload
FROM histo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment