WMI query slow

Jack 1 Reputation point
2021-04-12T03:34:26.387+00:00

stay mysql.data.dll Code, there is a query statement occasionally executed very slowly

MySql.Data.MySqlClient.MySqlConnectAttrs.OSDetails

ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = managementObjectCollection.GetEnumerator())
{
    if (enumerator.MoveNext())
    {
        ManagementBaseObject current = enumerator.Current;
        result = current.GetPropertyValue("Caption").ToString();
        }
    }
}
return result;

The execution time varies from a few seconds to dozens of seconds

Windows for business Windows Server User experience Other
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2021-04-12T09:01:32.88+00:00

    Hello @Jack

    I would suggest to post code related issue on Stack Overflow which focus more on programming,script and code for developers.
    Users there are more familiar with this issue and are better at solving it.

    Best Regards
    Karlie

    ----------

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Matthew Shea 1 Reputation point
    2022-04-09T15:59:17.09+00:00

    WMI is very slow. As I type this post, I am browsing Google for an explanation why, and possible ways to speed up my use of ManagementObjectSearcher.

    At the moment I have not found a perfect answer, but I can offer you this hint: do not use SELECT * when running WMI queries.

    The SELECT * returns all fields of data for the given provider, and the time adds up. You can reduce the query time by only using the fields you need.

    In the case of your example, you should use this instead:

     ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem");  
    

    For your reference, here is the list of available fields & types, and the link I got it from:
    https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-operatingsystem

      string   BootDevice;  
      string   BuildNumber;  
      string   BuildType;  
      string   Caption;  
      string   CodeSet;  
      string   CountryCode;  
      string   CreationClassName;  
      string   CSCreationClassName;  
      string   CSDVersion;  
      string   CSName;  
      sint16   CurrentTimeZone;  
      boolean  DataExecutionPrevention_Available;  
      boolean  DataExecutionPrevention_32BitApplications;  
      boolean  DataExecutionPrevention_Drivers;  
      uint8    DataExecutionPrevention_SupportPolicy;  
      boolean  Debug;  
      string   Description;  
      boolean  Distributed;  
      uint32   EncryptionLevel;  
      uint8    ForegroundApplicationBoost = 2;  
      uint64   FreePhysicalMemory;  
      uint64   FreeSpaceInPagingFiles;  
      uint64   FreeVirtualMemory;  
      datetime InstallDate;  
      uint32   LargeSystemCache;  
      datetime LastBootUpTime;  
      datetime LocalDateTime;  
      string   Locale;  
      string   Manufacturer;  
      uint32   MaxNumberOfProcesses;  
      uint64   MaxProcessMemorySize;  
      string   MUILanguages[];  
      string   Name;  
      uint32   NumberOfLicensedUsers;  
      uint32   NumberOfProcesses;  
      uint32   NumberOfUsers;  
      uint32   OperatingSystemSKU;  
      string   Organization;  
      string   OSArchitecture;  
      uint32   OSLanguage;  
      uint32   OSProductSuite;  
      uint16   OSType;  
      string   OtherTypeDescription;  
      Boolean  PAEEnabled;  
      string   PlusProductID;  
      string   PlusVersionNumber;  
      boolean  PortableOperatingSystem;  
      boolean  Primary;  
      uint32   ProductType;  
      string   RegisteredUser;  
      string   SerialNumber;  
      uint16   ServicePackMajorVersion;  
      uint16   ServicePackMinorVersion;  
      uint64   SizeStoredInPagingFiles;  
      string   Status;  
      uint32   SuiteMask;  
      string   SystemDevice;  
      string   SystemDirectory;  
      string   SystemDrive;  
      uint64   TotalSwapSpaceSize;  
      uint64   TotalVirtualMemorySize;  
      uint64   TotalVisibleMemorySize;  
      string   Version;  
      string   WindowsDirectory;  
      uint8    QuantumLength;  
      uint8    QuantumType;  
    
    0 comments No comments

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.