SELECT interval_end,
table_name,
write_count
FROM spanner_sys.table_operations_stats_minute
WHERE interval_end = (
SELECT MAX(interval_end)
FROM spanner_sys.table_operations_stats_minute)
ORDER BY write_count DESC;
查询最近间隔内删除操作次数最多的表和索引
SELECT interval_end,
table_name,
delete_count
FROM spanner_sys.table_operations_stats_minute
WHERE interval_end = (
SELECT MAX(interval_end)
FROM spanner_sys.table_operations_stats_minute)
ORDER BY delete_count DESC;
查询最近间隔内读取和查询操作次数最多的表和索引
SELECT interval_end,
table_name,
read_query_count
FROM spanner_sys.table_operations_stats_minute
WHERE interval_end = (
SELECT MAX(interval_end)
FROM spanner_sys.table_operations_stats_minute)
ORDER BY read_query_count DESC;
查询表在过去 6 小时内的使用情况
GoogleSQL
SELECT interval_end,
read_query_count,
write_count,
delete_count
FROM spanner_sys.table_operations_stats_minute
WHERE table_name = "table_name"
ORDER BY interval_end DESC;
其中:
table_name 必须是数据库中的现有表或索引。
PostgreSQL
SELECT interval_end,
read_query_count,
write_count,
delete_count
FROM spanner_sys.table_operations_stats_minute
WHERE table_name = 'table_name'
ORDER BY interval_end DESC;
其中:
table_name 必须是数据库中的现有表或索引。
查询表在过去 14 天内的使用情况
GoogleSQL
SELECT interval_end,
read_query_count,
write_count,
delete_count
FROM spanner_sys.table_operations_stats_hour
WHERE interval_end > TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL -14 DAY)
AND table_name = "table_name"
ORDER BY interval_end DESC;
其中:
table_name 必须是数据库中的现有表或索引。
PostgreSQL
SELECT interval_end,
read_query_count,
write_count,
delete_count
FROM spanner_sys.table_operations_stats_hour
WHERE interval_end > spanner.timestamptz_subtract(now(), '14 DAY')
AND table_name = 'table_name'
ORDER BY interval_end DESC;
其中:
table_name 必须是数据库中的现有表或索引。
查询过去 24 小时内未使用过的表和索引
GoogleSQL
(SELECT t.table_name
FROM information_schema.tables AS t
WHERE t.table_catalog = ""
AND t.table_schema = ""
AND t.table_type = "BASE TABLE"
UNION ALL
SELECT cs.change_stream_name
FROM information_schema.change_streams cs
WHERE cs.change_stream_catalog = ""
AND cs.change_stream_schema = ""
UNION ALL
SELECT idx.index_name
FROM information_schema.indexes idx
WHERE idx.index_type = "INDEX"
AND idx.table_catalog = ""
AND idx.table_schema = "")
EXCEPT ALL
(SELECT DISTINCT(table_name)
FROM spanner_sys.table_operations_stats_hour
WHERE interval_end > TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL -24 HOUR));
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-05。"],[],[],null,["# Table operations statistics\n\nSpanner provides built-in tables that records the read (or query),\nwrite, and delete operations statistics for your tables (including change\nstreams tables) and indexes. With table operations statistics you can do the\nfollowing:\n\n- Identify tables with increased write traffic corresponding\n to storage increase.\n\n- Identify tables with unexpected read, write, and delete traffic.\n\n- Identify heavily-used tables.\n\nWhen you query or write to a table, the corresponding operation count for the\ntable increments by 1, regardless of the number of rows accessed.\n\nOverall operations-per-second metrics of a database can be monitored with\n`Operations per second`, `Operations per second by API method`, and other\nrelated metrics in your [System Insights](/spanner/docs/monitoring-console)\ncharts.\n| **Note:** The sum of the operation counts on all tables and indexes might not be equal to the total operations on a database. For example, one write to a table increments the `write_count` on the table and on all indexes on the table. However, the write only counts as one operation on the database. The operation counts don't depend on the number of rows read or written to. They track the number of operations only. When the PartitionRead or PartitionQuery API returns multiple partition tokens, each Read or ExecuteSql call with a different token counts as a separate table operation.\n\nAccess table operations statistics\n----------------------------------\n\nSpanner provides the table operations statistics in the\n`SPANNER_SYS` schema.You can use the following ways to access `SPANNER_SYS` data:\n\n- A database's Spanner Studio page in the Google Cloud console.\n\n- The `gcloud spanner databases execute-sql` command.\n\n- The [`executeSql`](/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/executeSql)\n or the [`executeStreamingSql`](/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/executeStreamingSql)\n method.\n\nThe following single read methods that Spanner provides\ndon't support `SPANNER_SYS`:\n\n- Performing a strong read from a single row or multiple rows in a table.\n- Performing a stale read from a single row or multiple rows in a table.\n- Reading from a single row or multiple rows in a secondary index.\n\nTable operations statistics\n---------------------------\n\nThe following tables track the read (or query), write, and delete statistics on your\ntables and indexes during a specific time period:\n\n- `SPANNER_SYS.TABLE_OPERATIONS_STATS_MINUTE`: Operations during 1 minute intervals\n- `SPANNER_SYS.TABLE_OPERATIONS_STATS_10MINUTE`: Operations during 10 minute intervals\n- `SPANNER_SYS.TABLE_OPERATIONS_STATS_HOUR`: Operations during 1 hour intervals\n\nThese tables have the following properties:\n\n- Each table contains data for non-overlapping time intervals of the length that\n the table name specifies.\n\n- Intervals are based on clock times. 1 minute intervals start on the minute, 10\n minute intervals start every 10 minutes starting on the hour, and 1 hour\n intervals start on the hour.\n\n For example, at 11:59:30 AM, the most recent intervals available to SQL\n queries are:\n - **1 minute**: 11:58:00--11:58:59 AM\n - **10 minute**: 11:40:00--11:49:59 AM\n - **1 hour**: 10:00:00--10:59:59 AM\n\n### Schema for all table operations statistics tables\n\nIf you insert data into your database using mutations, the `write_count`\nincrements by 1 for each table accessed by the insert statement. In addition,\na query that accesses an index, without scanning the underlying table, only\nincrements the `read_query_count` on the index.\n\nData retention\n--------------\n\nAt a minimum, Spanner keeps data for each table for the following\ntime periods:\n\n- `SPANNER_SYS.TABLE_OPERATIONS_STATS_MINUTE`: Intervals covering the previous 6\n hours.\n\n- `SPANNER_SYS.TABLE_OPERATIONS_STATS_10MINUTE`: Intervals covering the previous\n 4 days.\n\n- `SPANNER_SYS.TABLE_OPERATIONS_STATS_HOUR`: Intervals covering the previous 30\n days.\n\n| **Note:** You cannot prevent Spanner from collecting table operations statistics. To delete the data in these tables, you must delete the database associated with the tables or wait until Spanner removes the data automatically.\n\n### Example queries\n\nThis section includes several example SQL statements that retrieve aggregate\ntable operations statistics. You can run these SQL statements using the\n[client libraries](/spanner/docs/reference/libraries), or the\n[gcloud spanner](/spanner/docs/gcloud-spanner#execute_sql_statements).\n\n#### Query the tables and indexes with the most write operations for the most recent interval\n\n```\n SELECT interval_end,\n table_name,\n write_count\n FROM spanner_sys.table_operations_stats_minute\n WHERE interval_end = (\n SELECT MAX(interval_end)\n FROM spanner_sys.table_operations_stats_minute)\n ORDER BY write_count DESC;\n \n```\n\n#### Query the tables and indexes with the most delete operations for the most recent interval\n\n```\n SELECT interval_end,\n table_name,\n delete_count\n FROM spanner_sys.table_operations_stats_minute\n WHERE interval_end = (\n SELECT MAX(interval_end)\n FROM spanner_sys.table_operations_stats_minute)\n ORDER BY delete_count DESC;\n \n```\n\n#### Query the tables and indexes with the most read and query operations for the most recent interval\n\n```\n SELECT interval_end,\n table_name,\n read_query_count\n FROM spanner_sys.table_operations_stats_minute\n WHERE interval_end = (\n SELECT MAX(interval_end)\n FROM spanner_sys.table_operations_stats_minute)\n ORDER BY read_query_count DESC;\n \n```\n\n#### Query the usage of a table over the last 6 hours\n\n### GoogleSQL\n\n```\n SELECT interval_end,\n read_query_count,\n write_count,\n delete_count\n FROM spanner_sys.table_operations_stats_minute\n WHERE table_name = \"table_name\"\n ORDER BY interval_end DESC;\n \n```\n\nWhere:\n\n- \u003cvar translate=\"no\"\u003e\u003ccode translate=\"no\" dir=\"ltr\"\u003etable_name\u003c/code\u003e\u003c/var\u003e must be an existing table or index in the database.\n\n### PostgreSQL\n\n```\n SELECT interval_end,\n read_query_count,\n write_count,\n delete_count\n FROM spanner_sys.table_operations_stats_minute\n WHERE table_name = 'table_name'\n ORDER BY interval_end DESC;\n \n```\n\nWhere:\n\n- \u003cvar translate=\"no\"\u003e\u003ccode translate=\"no\" dir=\"ltr\"\u003etable_name\u003c/code\u003e\u003c/var\u003e must be an existing table or index in the database.\n\n#### Query the usage of a table over the last 14 days\n\n### GoogleSQL\n\n```\nSELECT interval_end,\n read_query_count,\n write_count,\n delete_count\nFROM spanner_sys.table_operations_stats_hour\nWHERE interval_end \u003e TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL -14 DAY)\n AND table_name = \"table_name\"\nORDER BY interval_end DESC;\n```\n\nWhere:\n\n- \u003cvar translate=\"no\"\u003e\u003ccode translate=\"no\" dir=\"ltr\"\u003etable_name\u003c/code\u003e\u003c/var\u003e must be an existing table or index in the database.\n\n### PostgreSQL\n\n```\nSELECT interval_end,\n read_query_count,\n write_count,\n delete_count\nFROM spanner_sys.table_operations_stats_hour\nWHERE interval_end \u003e spanner.timestamptz_subtract(now(), '14 DAY')\n AND table_name = 'table_name'\nORDER BY interval_end DESC;\n```\n\nWhere:\n\n- \u003cvar translate=\"no\"\u003e\u003ccode translate=\"no\" dir=\"ltr\"\u003etable_name\u003c/code\u003e\u003c/var\u003e must be an existing table or index in the database.\n\n#### Query the tables and indexes with no usage in the last 24 hours\n\n### GoogleSQL\n\n```\n(SELECT t.table_name\n FROM information_schema.tables AS t\n WHERE t.table_catalog = \"\"\n AND t.table_schema = \"\"\n AND t.table_type = \"BASE TABLE\"\n UNION ALL\n SELECT cs.change_stream_name\n FROM information_schema.change_streams cs\n WHERE cs.change_stream_catalog = \"\"\n AND cs.change_stream_schema = \"\"\n UNION ALL\n SELECT idx.index_name\n FROM information_schema.indexes idx\n WHERE idx.index_type = \"INDEX\"\n AND idx.table_catalog = \"\"\n AND idx.table_schema = \"\")\n EXCEPT ALL\n(SELECT DISTINCT(table_name)\n FROM spanner_sys.table_operations_stats_hour\n WHERE interval_end \u003e TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL -24 HOUR));\n```\n\nWhat's next\n-----------\n\n- Use [Table sizes statistics](/spanner/docs/introspection/table-sizes-statistics)\n to determine the sizes of your tables and indexes.\n\n- Learn about other [Introspection tools](/spanner/docs/introspection).\n\n- Learn more about [SQL best practices](/spanner/docs/sql-best-practices) for\n Spanner."]]