Could someone kindly help me to set the max memory for SQL via powershell

Jimmy Afflick 80 Reputation points
2024-06-06T06:45:34.3766667+00:00

Hi Experts,

Could someone please help me to set the max memory in SQL for the below request.

maxmerory

I have checked the below link. But I am not able to make out. Because I am not expertise in scripting and PowerShell. So need your help.
https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/server-memory-server-configuration-options?view=sql-server-ver16

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,970 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,210 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,549 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,581 questions
0 comments No comments
{count} votes

Accepted answer
  1. MikeyQiaoMSFT-0444 3,190 Reputation points
    2024-06-06T08:29:37.04+00:00

    Hi,Jimmy Afflick

    You can set the maximum memory through the UI prompts of SQL Server Management Studio(Make sure that the user you are using to connect to the database has the 'sysadmin' role.):User's imageUser's image

    You can use sqlcmd to pass T-SQL commands to modify the maximum memory.

    1.Check the sqlcmd version information.

    Get-Command sqlcmd
    

    User's image

    2.Log in to the database.

    Windows Authentication:

    sqlcmd -S .\INSTANCENAME -E
    

    Account Password Verification:

    sqlcmd -S .\INSTANCENAME -U YourUserName -P YourPassword
    
    

    User's image

    3.Enable advanced options (if not already enabled)

    EXEC sp_configure 'show advanced options', 1; 
    RECONFIGURE;
    GO
    
    

    User's image

    4.Set the maximum memory (in MB).

    EXEC sp_configure 'max server memory', 4096;
    RECONFIGURE;
    GO
    

    User's image


2 additional answers

Sort by: Most helpful
  1. Olaf Helper 45,091 Reputation points
    2024-06-06T08:45:20.7933333+00:00

    If it should be with PowerShell then use DbaTools CmdLet:

    https://docs.dbatools.io/Set-DbaMaxMemory.html

    1 person found this answer helpful.
    0 comments No comments

  2. Erland Sommarskog 112.7K Reputation points MVP
    2024-06-06T13:23:15.55+00:00

    There is not really a simple formula from the total RAM in the server to how you should set "max server memory". Well, you can set up a formula if the assumption is that there will be exactly one instance of SQL Server running on the machine, and nohting else would run on the server.

    But if you have a mixed workload, that includes other SQL Server instances, Integration Services, Repotring Services etc, you need to account for these as well.

    I would say that that the table you have found is not suitable for the case when you have SQL Server alone on the server, because it only gives 50% of the memory to SQL Server, and that's way to less. I would go for 85-90% in that situation.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.