ここで考えられるアイデア:-
SELECT FirstLetter, MAX(name), SUM(NameCount)
FROM
(
SELECT substr(name, 1, 1) AS FirstLetter, MIN(name) AS name, COUNT(*) AS NameCount
FROM company
GROUP BY FirstLetter
UNION
SELECT 'A' AS FirstLetter, "" AS name, 0 AS NameCount
UNION
SELECT 'B' AS FirstLetter, "" AS name, 0 AS NameCount
UNION
SELECT 'C' AS FirstLetter, "" AS name, 0 AS NameCount
UNION
SELECT 'D' AS FirstLetter, "" AS name, 0 AS NameCount
UNION
SELECT 'E' AS FirstLetter, "" AS name, 0 AS NameCount
UNION
SELECT 'F' AS FirstLetter, "" AS name, 0 AS NameCount
UNION
SELECT 'G' AS FirstLetter, "" AS name, 0 AS NameCount
UNION
SELECT 'H' AS FirstLetter, "" AS name, 0 AS NameCount
UNION
SELECT 'I' AS FirstLetter, "" AS name, 0 AS NameCount
UNION
SELECT 'J' AS FirstLetter, "" AS name, 0 AS NameCount
UNION
SELECT 'K' AS FirstLetter, "" AS name, 0 AS NameCount
UNION
SELECT 'L' AS FirstLetter, "" AS name, 0 AS NameCount
) sub1
GROUP BY FirstLetter
(ギャップを埋めるために結合できる文字を入力するのに飽きました)。
これは機能しますが、あなたのサイズのテーブルでのパフォーマンスはわかりません(約14万レコードのランダムテーブル/フィールドで1秒未満かかります)。
編集 - OK 再試行します。
基本的なクエリは次のようになります (空白を埋めることは無視します):-
SELECT CASE WHEN name REGEXP '[A-Za-z].*' THEN UPPER(SUBSTR(name, 1, 1)) ELSE '@' END as formatted_letter, MIN(id) AS id, COUNT(*) AS NameCount
FROM users
GROUP BY formatted_letter
これは、それ自体でかなり効率的なはずです。試してみて、所要時間をお知らせください。
それが速い場合は、組合がゼロカウントレコードを追加するために公称時間を追加する必要があります。
140k レコードのランダム テーブルで試してみると、約 1 秒かかります (名前フィールドはインデックス化されていません)。
結合された選択を追加しても、クエリに顕著な時間が追加されることはありません:-
SELECT formatted_letter, MAX(name), SUM(NameCount)
FROM
(
SELECT CASE WHEN company REGEXP '[A-Za-z].*' THEN UPPER(SUBSTR(company, 1, 1)) ELSE '@' END as formatted_letter, MIN(id) AS id, COUNT(*) AS NameCount
FROM users
GROUP BY formatted_letter
UNION
SELECT 'A' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'B' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'C' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'D' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'E' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'F' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'G' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'H' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'I' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'J' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'K' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'L' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'M' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'N' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'O' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'P' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'Q' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'R' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'S' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'T' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'U' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'V' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'W' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'X' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'Y' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT 'Z' AS formatted_letter, "" AS id, 0 AS NameCount
UNION SELECT '@' AS formatted_letter, "" AS id, 0 AS NameCount
) Sub1
GROUP BY formatted_letter
お使いのマシンでこれに 36 秒程度かかる場合は、おかしなことが起こっています。