Stay organized with collections Save and categorize content based on your preferences.
Cloud SQL federated queriesAs a data analyst, you can query data in Cloud SQL from BigQuery using federated queries.
BigQuery Cloud SQL federation enables BigQuery to query data residing in Cloud SQL in real time, without copying or moving data. Query federation supports both MySQL (2nd generation) and PostgreSQL instances in Cloud SQL.
Alternatively, to replicate data into BigQuery, you can also use Cloud Data Fusion or Datastream. For more about using Cloud Data Fusion, see Replicating data from MySQL to BigQuery.
Before you beginTo get the permissions that you need to query a Cloud SQL instance, ask your administrator to grant you the BigQuery Connection User (roles/bigquery.connectionUser
) IAM role on your project. For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
To send a federated query to Cloud SQL from a GoogleSQL query, use the EXTERNAL_QUERY
function.
Suppose that you store a customer table in BigQuery, while storing a sales table in Cloud SQL, and want to join the two tables in a single query. The following example makes a federated query to a Cloud SQL table named orders
and joins the results with a BigQuery table named mydataset.customers
.
SELECT c.customer_id, c.name, rq.first_order_date
FROM mydataset.customers AS c
LEFT OUTER JOIN EXTERNAL_QUERY(
'us.connection_id',
'''SELECT customer_id, MIN(order_date) AS first_order_date
FROM orders
GROUP BY customer_id''') AS rq ON rq.customer_id = c.customer_id
GROUP BY c.customer_id, c.name, rq.first_order_date;
The example query includes 3 parts:
SELECT customer_id, MIN(order_date) AS first_order_date FROM orders GROUP BY customer_id
in the operational PostgreSQL database to get the first order date for each customer through the EXTERNAL_QUERY()
function.customer_id
.You can use the EXTERNAL_QUERY()
function to query information_schema tables to access database metadata, such as list all tables in the database or show table schema. The following example information_schema queries work in both MySQL and PostgreSQL. You can learn more from MySQL information_schema tables and PostgreSQL information_schema tables.
-- List all tables in a database.
SELECT * FROM EXTERNAL_QUERY("connection_id",
"select * from information_schema.tables;");
-- List all columns in a table.
SELECT * FROM EXTERNAL_QUERY("connection_id",
"select * from information_schema.columns where table_name='x';");
Connection details
The following table shows the Cloud SQL connection properties:
Property name Value Descriptionname
string Name of the connection resource in the format: project_id.location_id.connection_id. location
string Location of the connection which must either match the Cloud SQL instance location or be a multi-region of the corresponding jurisdiction. For example, a Cloud SQL instance in us-east4
can use US
, while a Cloud SQL instance in europe-north1
can use EU
. Only BigQuery queries running in this location will be able to use this connection. friendlyName
string A user-friendly display name for the connection. description
string Description of the connection. cloudSql.type
string Can be "POSTGRES" or "MYSQL". cloudSql.instanceId
string Name of the Cloud SQL instance, usually in the format of:
Project-id:location-id:instance-id
You can find the instance ID in the Cloud SQL instance detail page.
cloudSql.database
string The Cloud SQL database that you want to connect to. cloudSql.serviceAccountId
string The service account configured to access the Cloud SQL database.
The following table shows the properties for the Cloud SQL instance credential:
Property name Value Descriptionusername
string Database username password
string Database password Track BigQuery federated queries
When you run a federated query against Cloud SQL, BigQuery annotates the query with a comment similar to the following:
/* Federated query from BigQuery. Project ID: PROJECT_ID, BigQuery Job ID: JOB_ID. */
If you are monitoring logs for query usage on a MySQL or PostgreSQL database, the following annotation can help you identify queries coming from BigQuery.
Go to the Logs Explorer page.
In the Query tab, enter the following query:
resource.type="cloudsql_database"
textPayload=~"Federated query from BigQuery"
Click Run query.
If there are records available for BigQuery federated queries, a list of records similar to the following appears in Query results:
YYYY-MM-DD hh:mm:ss.millis UTC [3210064]: [4-1] db=DATABASE, user=USER_ACCOUNT STATEMENT: SELECT 1 FROM (SELECT FROM company_name_table) t; /* Federated query from BigQuery. Project ID: PROJECT_ID, BigQuery Job ID: JOB_ID */ YYYY-MM-DD hh:mm:ss.millis UTC [3210532]: [2-1] db=DATABASE, user=USER_ACCOUNT STATEMENT: SELECT "company_id", "company type_id" FROM (SELECT FROM company_name_table) t; /* Federated query from BigQuery. Project ID: PROJECT_ID, BigQuery Job ID: JOB_ID */
This section helps you troubleshoot issues you might encounter when sending a federated query to Cloud SQL.
Issue: Failed to connect to database server. If you are querying a MySQL database, you might encounter the following error:
Invalid table-valued function EXTERNAL_QUERY Failed to connect to MySQL database. Error: MysqlErrorCode(2013): Lost connection to MySQL server during query.
Alternatively, if you are querying a PostgreSQL database, you might encounter the following error:
Invalid table-valued function EXTERNAL_QUERY Connect to PostgreSQL server failed: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.
roles/cloudsql.client
) role. The service account is of the following format: service-PROJECT_NUMBER@gcp-sa-bigqueryconnection.iam.gserviceaccount.com
. For detailed instructions, see Grant access to the service account.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["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-08-07 UTC."],[[["Cloud SQL federated queries allow data analysts to query data in Cloud SQL directly from BigQuery in real time, without the need to copy or move the data, supporting both MySQL and PostgreSQL instances."],["To perform a federated query, you must use the `EXTERNAL_QUERY` function within a GoogleSQL query, and a Cloud SQL connection must have been created and shared with you by your administrator, while ensuring you have the required BigQuery Connection User IAM role."],["You can join data from a Cloud SQL table with a BigQuery table in a single query using the `EXTERNAL_QUERY` function, as demonstrated by an example joining a Cloud SQL `orders` table with a BigQuery `customers` table."],["The `EXTERNAL_QUERY()` function can also be used to access Cloud SQL database metadata, such as listing all tables or showing a specific table's schema by querying the `information_schema` tables."],["When a federated query is run, BigQuery annotates the query with a comment including the Project ID and BigQuery Job ID, which can be used to identify these queries in the database logs using the Logs Explorer."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4