Eliminazione collettiva con DML partizionato
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Elimina collettivamente i dati utilizzando un'istruzione DML partizionata.
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],[],[],[],null,["Bulk delete data by using a partitioned DML statement.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Insert, update, and delete data using data manipulation language (DML)](/spanner/docs/dml-tasks)\n- [Partitioned Data Manipulation Language](/spanner/docs/dml-partitioned)\n- [Transactions overview](/spanner/docs/transactions)\n\nCode sample \n\nC++\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n void DmlPartitionedDelete(google::cloud::spanner::Client client) {\n namespace spanner = ::google::cloud::spanner;\n auto result = client.ExecutePartitionedDml(\n spanner::SqlStatement(\"DELETE FROM Singers WHERE SingerId \u003e 10\"));\n if (!result) throw std::move(result).status();\n std::cout \u003c\u003c \"Deleted at least \" \u003c\u003c result-\u003erow_count_lower_bound\n \u003c\u003c \" row(s) [spanner_dml_partitioned_delete]\\n\";\n }\n\nC#\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n using https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.Data/latest/Google.Cloud.Spanner.Data.html;\n using System;\n using System.Threading.Tasks;\n\n public class DeleteUsingPartitionedDmlCoreAsyncSample\n {\n public async Task\u003clong\u003e DeleteUsingPartitionedDmlCoreAsync(string projectId, string instanceId, string databaseId)\n {\n string connectionString = $\"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}\";\n\n using var connection = new https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.Data/latest/Google.Cloud.Spanner.Data.SpannerConnection.html(connectionString);\n await connection.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.Data/latest/Google.Cloud.Spanner.Data.SpannerConnection.html#Google_Cloud_Spanner_Data_SpannerConnection_OpenAsync_Google_Cloud_Spanner_Data_SpannerTransactionCreationOptions_Google_Cloud_Spanner_Data_SpannerTransactionOptions_System_Threading_CancellationToken_();\n\n using var cmd = connection.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.Data/latest/Google.Cloud.Spanner.Data.SpannerConnection.html#Google_Cloud_Spanner_Data_SpannerConnection_CreateDmlCommand_System_String_Google_Cloud_Spanner_Data_SpannerParameterCollection_(\"DELETE FROM Singers WHERE SingerId \u003e 10\");\n long rowCount = await cmd.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Spanner.Data/latest/Google.Cloud.Spanner.Data.SpannerCommand.html#Google_Cloud_Spanner_Data_SpannerCommand_ExecutePartitionedUpdateAsync_System_Threading_CancellationToken_();\n\n Console.WriteLine($\"{rowCount} row(s) deleted...\");\n return rowCount;\n }\n }\n\nGo\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/spanner\"\n )\n\n func deleteUsingPartitionedDML(w io.Writer, db string) error {\n \tctx := context.Background()\n \tclient, err := spanner.NewClient(ctx, db)\n \tif err != nil {\n \t\treturn err\n \t}\n \tdefer client.Close()\n\n \tstmt := spanner.https://cloud.google.com/go/docs/reference/cloud.google.com/go/spanner/latest/index.html#cloud_google_com_go_spanner_Statement{SQL: \"DELETE FROM Singers WHERE SingerId \u003e 10\"}\n \trowCount, err := client.PartitionedUpdate(ctx, stmt)\n \tif err != nil {\n \t\treturn err\n\n \t}\n \tfmt.Fprintf(w, \"%d record(s) deleted.\", rowCount)\n \treturn nil\n }\n\nJava\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n static void deleteUsingPartitionedDml(DatabaseClient dbClient) {\n String sql = \"DELETE FROM Singers WHERE SingerId \u003e 10\";\n long rowCount = dbClient.executePartitionedUpdate(Statement.of(sql));\n System.out.printf(\"%d records deleted.\\n\", rowCount);\n }\n\nNode.js\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // Imports the Google Cloud client library\n const {Spanner} = require('https://cloud.google.com/nodejs/docs/reference/spanner/latest/overview.html');\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample.\n */\n // const projectId = 'my-project-id';\n // const instanceId = 'my-instance';\n // const databaseId = 'my-database';\n\n // Creates a client\n const spanner = new https://cloud.google.com/nodejs/docs/reference/spanner/latest/spanner/spanner.html({\n projectId: projectId,\n });\n\n // Gets a reference to a Cloud Spanner instance and database\n const instance = spanner.instance(instanceId);\n const database = instance.database(databaseId);\n\n try {\n const [rowCount] = await database.https://cloud.google.com/nodejs/docs/reference/spanner/latest/spanner/database.html({\n sql: 'DELETE FROM Singers WHERE SingerId \u003e 10',\n });\n console.log(`Successfully deleted ${rowCount} records.`);\n } catch (err) {\n console.error('ERROR:', err);\n } finally {\n // Close the database when finished.\n database.close();\n }\n\nPHP\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n use Google\\Cloud\\Spanner\\SpannerClient;\n\n /**\n * Delete sample data in the database by partition with a DML statement.\n *\n * This updates the `MarketingBudget` column which must be created before\n * running this sample. You can add the column by running the `add_column`\n * sample or by running this DDL statement against your database:\n *\n * ALTER TABLE Albums ADD COLUMN MarketingBudget INT64\n *\n * Example:\n * ```\n * update_data($instanceId, $databaseId);\n * ```\n *\n * @param string $instanceId The Spanner instance ID.\n * @param string $databaseId The Spanner database ID.\n */\n function delete_data_with_partitioned_dml(string $instanceId, string $databaseId): void\n {\n $spanner = new SpannerClient();\n $instance = $spanner-\u003einstance($instanceId);\n $database = $instance-\u003edatabase($databaseId);\n\n $rowCount = $database-\u003eexecutePartitionedUpdate(\n 'DELETE FROM Singers WHERE SingerId \u003e 10'\n );\n\n printf('Deleted %d row(s).' . PHP_EOL, $rowCount);\n }\n\nPython\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n # instance_id = \"your-spanner-instance\"\n # database_id = \"your-spanner-db-id\"\n spanner_client = spanner.Client()\n instance = spanner_client.instance(instance_id)\n database = instance.database(database_id)\n\n row_ct = database.execute_partitioned_dml(\"DELETE FROM Singers WHERE SingerId \u003e 10\")\n\n print(\"{} record(s) deleted.\".format(row_ct))\n\nRuby\n\n\nTo learn how to install and use the client library for Spanner, see\n[Spanner client libraries](/spanner/docs/reference/libraries).\n\n\nTo authenticate to Spanner, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n # project_id = \"Your Google Cloud project ID\"\n # instance_id = \"Your Spanner instance ID\"\n # database_id = \"Your Spanner database ID\"\n\n require \"google/cloud/spanner\"\n\n spanner = Google::Cloud::https://cloud.google.com/ruby/docs/reference/activerecord-spanner-adapter/latest/Google-Cloud-Spanner.html.new project: project_id\n client = spanner.https://cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Project.html instance_id, database_id\n\n row_count = client.https://cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client.html(\n \"DELETE FROM Singers WHERE SingerId \u003e 10\"\n )\n\n puts \"#{row_count} records deleted.\"\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=spanner)."]]