Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
MySQL | PostgreSQL | SQL Server
Auf dieser Seite wird die Verwaltung der tempdb-Datenbank in Cloud SQL beschrieben.
Die tempdb-Datenbank ist eine Systemdatenbank, die viele Objekte enthält, einschließlich temporärer Tabellen, gespeicherter Prozeduren und mehr. In Ihren Instanzen können Sie übliche Vorgänge für diese Datenbank ausführen.
Eine tempdb-Datenbank wird bei jedem Neustart einer Instanz neu erstellt. Damit keine Nutzerberechtigungen verloren gehen, stellt Cloud SQL dem Nutzer sqlserver nach dem Neustart einer Instanz Berechtigungen bereit.
Überblick
Der Nutzersqlserver hat die Berechtigung ALTER zum Verwalten der temporären Datenbankoptionen.
Weitere Informationen zum Verwalten dieser Ressource finden Sie auf der Seite tempdb-Datenbank.
tempdb-Dateien verwalten
Nachdem Sie die Verbindung zu einer Instanz hergestellt haben, kann der Nutzersqlserver die temporären Dateien verwalten.
Anzahl der Dateien
Der Nutzer hat die Berechtigung ALTER für die temporäre Datenbank, mit der er Einstellungen für die Anzahl der Dateien und mehr steuern kann. Beispiele für Vorgänge:
ALTER DATABASE [tempdb] ADD FILE
ALTER DATABASE [tempdb] REMOVE
Dateigröße
In den folgenden Abschnitten werden Methoden zum Steuern der Größe von Dateien in der tempdb-Datenbank beschrieben.
msdb.dbo.gcloudsql_tempdb_shrinkfile ist eine gespeicherte Prozedur, mit der Sie eine einzelne Datei in der Datenbank tempdb verkleinern können.
Diese gespeicherte Prozedur bietet alle Vorteile des Befehls DBCC SHRINKFILE.
Im Folgenden finden Sie Beispiele für die Verwendung der gespeicherten Prozedur msdb.dbo.gcloudsql_tempdb_shrinkfile und ihrer Parameter, die über Cloud SQL Studio ausgeführt werden:
EMPTY_FILE_INT: Ein Ganzzahlwert, der entweder 0 oder 1 ist. Wenn 1, wird EMPTYFILE als Option übergeben. Diese Option hat Vorrang vor anderen Parametern.
Mit diesem Befehl werden die folgenden SQL Server-Befehle ausgeführt:
TARGET_SIZE_INT: Eine Ganzzahl, die die Zielgröße der Datei in Megabyte darstellt. Übergibt an den DBCC SHRINKFILE-Befehl alle Werte, die größer oder gleich null sind. Beispiel: 10
Mit diesem Befehl werden die folgenden SQL Server-Befehle ausgeführt. Die Ganzzahl 10 ist als Beispiel enthalten:
TRUNCATE_ONLY_INT: Akzeptiert einen Ganzzahlwert von „0“ oder „1“.
Wenn der Wert auf 1 gesetzt ist, wird TRUNCATEONLY als Option übergeben. Die Zielgröße wird ignoriert, wenn TRUNCATEONLY übergeben wird. Diese Option hat Vorrang vor NOTRUNCATE.
Mit diesem Befehl werden die folgenden SQL Server-Befehle ausgeführt:
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[],[],null,["# Manage a tempdb database\n\n\u003cbr /\u003e\n\nMySQL \\| PostgreSQL \\| SQL Server\n\n\u003cbr /\u003e\n\nThis page describes management of the tempdb database in Cloud SQL.\n\nThe tempdb database is a system database that holds many objects, including\ntemporary tables, stored procedures, and more. In your instances, you can\nperform common operations on this database.\n\nA tempdb database is recreated each time an instance is restarted. To prevent\nthe loss of user permissions, Cloud SQL provides permissions to the `sqlserver`\nuser after an instance is restarted.\n\nOverview\n--------\n\nThe `sqlserver` [user](/sql/docs/sqlserver/users)\nhas the ALTER permission for managing the tempdb database options.\n\nFor more information about managing this resource, see the [tempdb database](https://docs.microsoft.com/en-us/sql/relational-databases/databases/tempdb-database)\npage.\n\nManage tempdb files\n-------------------\n\nAfter you [connect](/sql/docs/sqlserver/connect-overview) to an instance, the\n`sqlserver` [user](/sql/docs/sqlserver/users) can manage the tempdb files.\n\nNumber of files\n---------------\n\nThe user has ALTER permission on the tempdb database, which lets them control\nsettings for the number of files and more. Some example operations include the\nfollowing:\n\n- `ALTER DATABASE [tempdb] ADD FILE`\n- `ALTER DATABASE [tempdb] REMOVE`\n\n| **Note:** See [Physical properties of tempdb in SQL Server](https://docs.microsoft.com/en-us/sql/relational-databases/databases/tempdb-database?view=sql-server-ver15#physical-properties-of-tempdb-in-sql-server), which includes recommendations related to these settings. Review the information about the number of tempdb data files that would correspond to a given number of logical processors. By default, Cloud SQL creates a minimum of eight files for instances that have greater than or equal to eight logical processors. As included in those recommendations, if the number of logical processors is fewer than or equal to eight, the number of files created is equal to the number of logical processors.\n\nFile size\n---------\n\nThe following sections describe methods used to control the size of files in the\n`tempdb` database.\n\nFor more information about these methods, see\n[Shrink the tempdb database](https://learn.microsoft.com/en-us/sql/relational-databases/databases/shrink-tempdb-database?view=sql-server-ver16).\n\n### Change file sizes in tempdb\n\nTo control the size of files in the tempdb database, use the `ALTER DATABASE`\nstatement. For more information, see\n[ALTER DATABASE (Transact-SQL) File and Filegroup Options](https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-file-and-filegroup-options?redirectedfrom=MSDN&view=sql-server-ver15).\n\n### Shrink individual file size\n\n`msdb.dbo.gcloudsql_tempdb_shrinkfile` is a [stored procedure](https://learn.microsoft.com/en-us/sql/relational-databases/stored-procedures/stored-procedures-database-engine?view=sql-server-ver16)\nyou can use to shrink an individual file in the `tempdb` database.\n\nThis stored procedure provides all the same benefits of the\n[`DBCC SHRINKFILE`](https://learn.microsoft.com/en-us/sql/t-sql/database-console-commands/dbcc-shrinkfile-transact-sql?view=sql-server-ver16)\ncommand.\n| **Caution:** Avoid using shrinkfile operations as a part of regular maintenance; only use when necessary. For more information, see [`DBCC SHRINKFILE`](https://learn.microsoft.com/en-us/sql/t-sql/database-console-commands/dbcc-shrinkfile-transact-sql?view=sql-server-ver16) documentation.\n\nThe following are example uses of the `msdb.dbo.gcloudsql_tempdb_shrinkfile`\nstored procedure and its parameters, executed from the [Cloud SQL Studio](/sql/docs/sqlserver/manage-data-using-studio):\n\n1. Default option\n\n ```sql\n msdb.dbo.gcloudsql_tempdb_shrinkfile @filename = '\u003cvar translate=\"no\"\u003eFILENAME\u003c/var\u003e'\n ```\n\n Where:\n - \u003cvar translate=\"no\"\u003eFILENAME\u003c/var\u003e: the name of the file to be shrunk. For example, `tempdev`.\n\n This command executes the following SQL Server commands:\n - `USE tempdb`\n - `DBCC SHRINKFILE (@filename)`\n2. `EMPTYFILE`\n\n ```sql\n msdb.dbo.gcloudsql_tempdb_shrinkfile @filename = '\u003cvar translate=\"no\"\u003eFILENAME\u003c/var\u003e', @empty_file=EMPTY_FILE_INT\n ```\n\n Where:\n - \u003cvar translate=\"no\"\u003eEMPTY_FILE_INT\u003c/var\u003e: an integer value that is either 0 or 1. If 1, then `EMPTYFILE` is passed as an option. This option takes priority over other parameters.\n\n This command executes the following SQL Server commands:\n - `USE tempdb`\n - `DBCC SHRINKFILE (@filename, EMPTYFILE)`\n3. Target size\n\n ```sql\n msdb.dbo.gcloudsql_tempdb_shrinkfile @filename = '\u003cvar translate=\"no\"\u003eFILENAME\u003c/var\u003e', @target_size=TARGET_SIZE_INT\n ```\n\n Where:\n - \u003cvar translate=\"no\"\u003eTARGET_SIZE_INT\u003c/var\u003e: an integer that represents the target size of the file in megabytes. Passes to the `DBCC SHRINKFILE` command any value that is greater than or equal to zero. For example, `10`.\n\n This command executes the following SQL Server commands. The integer 10 is\n included as an example:\n - `USE tempdb`\n - `DBCC SHRINKFILE (@filename, 10)`\n4. Target size and truncate only\n\n ```sql\n msdb.dbo.gcloudsql_tempdb_shrinkfile @filename = '\u003cvar translate=\"no\"\u003eFILENAME\u003c/var\u003e', @target_size=10, @truncateonly=TRUNCATE_ONLY_INT\n ```\n\n Where:\n - \u003cvar translate=\"no\"\u003eTRUNCATE_ONLY_INT\u003c/var\u003e: accepts an integer value of either 0 or 1. If set to 1, then `TRUNCATEONLY` is passed as an option. Target size is ignored if `TRUNCATEONLY` is passed. This option takes priority over `NOTRUNCATE`.\n\n This command executes the following SQL Server commands:\n - `USE tempdb`\n - `DBCC SHRINKFILE (@filename, 10, TRUNCATEONLY)`\n5. Target size and no truncate option\n\n ```sql\n msdb.dbo.gcloudsql_tempdb_shrinkfile @filename = '\u003cvar translate=\"no\"\u003eFILENAME\u003c/var\u003e', @target_size=10, @no_truncate=NO_TRUNCATE_INT\n ```\n\n Where:\n - \u003cvar translate=\"no\"\u003eNO_TRUNCATE_INT\u003c/var\u003e: accepts an integer value of either 0 or 1. If set to 1, then `NOTRUNCATE` is passed as an option.\n\n This command executes the following SQL Server commands:\n - `USE tempdb`\n - `DBCC SHRINKFILE (@filename, 10, NOTRUNCATE)`\n\nWhat's next\n-----------\n\n- Learn about [Instance settings](/sql/docs/sqlserver/instance-settings).\n- Learn about [Monitoring Cloud SQL instances](/sql/docs/sqlserver/monitor-instance)."]]