When you query external datasets, columnar data is automatically used if it's
available and suitable for your query.
If you use theEXTERNAL_QUERYfunction, include the@{scan_method=columnar}hint in the nested Spanner query.
In the following example:
The first argument toEXTERNAL_QUERYspecifies the external connection and
dataset,my-project.us.albums.
The second argument is a SQL query that selectsMarketingBudgetfrom theAlbumInfotable whereMarketingBudgetis less than 500,000.
The@{scan_method=columnar}hint optimizes the external query for columnar
scanning.
The outerSELECTstatement calculates the sum of theMarketingBudgetvalues returned by the external query.
TheAS total_marketing_spendclause assigns an alias to the calculated
sum.
SELECTSUM(MarketingBudget)AStotal_marketing_spendFROMEXTERNAL_QUERY('my-project.us.albums','@{scan_method=columnar} SELECT AlbumInfo.MarketingBudget FROM AlbumInfo WHERE AlbumInfo.MarketingBudget < 500000;');
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Query columnar data\n\n| **Preview\n| --- [Spanner columnar engine](/spanner/docs/columnar-engine)**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n|\n|\n| For information about access to this\n| release, see the\n| [access request page](https://forms.gle/nE1bAqfHJrqhkStR9).\n\n\n| **PostgreSQL interface note:** The examples in this topic are intended for GoogleSQL-dialect databases. This feature doesn't support PostgreSQL interface.\n\n\u003cbr /\u003e\n\nThis page describes how to run queries against columnar data.\n\nQuery columnar data\n-------------------\n\nThe `@{scan_method=columnar}` query hint enables a query to read columnar data.\nYou can set the `scan_method` hint at the\n[statement level](/spanner/docs/reference/standard-sql/query-syntax#statement_hints)\nor at the [table level](/spanner/docs/reference/standard-sql/query-syntax#table_hints).\n\nFor example, you can use the following queries to read columnar data from the\n`Singers` and `Messages` table:\n\n- `@{scan_method=columnar} SELECT COUNT(*) FROM Singers;`\n- `SELECT COUNT(*) FROM Singers @{scan_method=columnar};`\n- `@{scan_method=columnar} SELECT m.MsgBlob FROM Messages WHERE m.id='1234';`\n\nQuery Spanner columnar data using BigQuery federated queries\n------------------------------------------------------------\n\nTo read Spanner columnar data from BigQuery, you can\neither create an [external dataset](/bigquery/docs/spanner-external-datasets#create_an_external_dataset)\nor use the\n[`EXTERNAL_QUERY`](/bigquery/docs/reference/standard-sql/federated_query_functions#external_query)\nfunction.\n\nWhen you query external datasets, columnar data is automatically used if it's\navailable and suitable for your query.\n\nIf you use the `EXTERNAL_QUERY` function, include the `@{scan_method=columnar}`\nhint in the nested Spanner query.\n\nIn the following example:\n\n- The first argument to `EXTERNAL_QUERY` specifies the external connection and dataset, `my-project.us.albums`.\n- The second argument is a SQL query that selects `MarketingBudget` from the `AlbumInfo` table where `MarketingBudget` is less than 500,000.\n- The `@{scan_method=columnar}` hint optimizes the external query for columnar scanning.\n- The outer `SELECT` statement calculates the sum of the `MarketingBudget` values returned by the external query.\n- The `AS total_marketing_spend` clause assigns an alias to the calculated sum.\n\n SELECT SUM(MarketingBudget) AS total_marketing_spend\n FROM\n EXTERNAL_QUERY(\n 'my-project.us.albums',\n '@{scan_method=columnar} SELECT AlbumInfo.MarketingBudget FROM AlbumInfo WHERE AlbumInfo.MarketingBudget \u003c 500000;');\n\nWhat's next\n-----------\n\n- Learn about [columnar engine](/spanner/docs/columnar-engine).\n- Learn how to [enable columnar engine](/spanner/docs/configure-columnar-engine).\n- Learn how to [monitor columnar engine](/spanner/docs/monitor-columnar-engine)."]]