Categories
Articles Windows Windows Server

Clearing IIS Logs at Regular Intervals on Windows Server

Internet Information Services (IIS) is a component that provides web server services on Windows Server. IIS records log files to monitor website performance and detect issues. However, over time, these log files can unnecessarily consume disk space. In this guide, we’ll look at how to clean up IIS logs on Windows Server.

Step 1: Determine the Location of IIS Logs

1.Log in to the Windows Server.

2.Open the “Internet Information Services (IIS) Manager” by typing “IIS” in the Start menu.

IIS 01
IIS 01

3.Expand the name of your server in the left-hand connection tree.

4.Under Sites, select the web site you want to clean up.

IIS 02
IIS 02

5.In the “IIS” section on the right side, find the “Logging” item. Here, you’ll see the directory where log files are stored. This is typically “C:\inetpub\logs\LogFiles” or a similar path.

IIS 03
IIS 03

Step 2: Cleaning Up Logs

1.Before deleting log files, you may want to back up their contents. You can copy the log files to another folder for this purpose.

2.Navigate to the “LogFiles” folder to clean up log files.

LogFiles
LogFiles

3.Here, you’ll see different folders named with date and time information (e.g., W3SVC1, W3SVC2, etc.). Choose the folder corresponding to the web site you’re interested in.

4.Select the log files within the folder, and press the Delete key to permanently remove the files.

Step 3: Creating a Scheduled Task (Optional)

If you want to clean up IIS logs periodically, you can use the Windows Task Scheduler. This way, you can automatically clean up logs at specified intervals.

  1. We’ll continue using the script we prepared earlier. Copy the script to a text editor, give it a name, and save it as a .vbs file.
sLogFolder = "c:\inetpub\logs\LogFiles"
iMaxAge = 30   'in days

' FileSystemObject oluşturuluyor
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Log dosyalarının bulunduğu klasörün alt klasörleri taranıyor
set colFolder = objFSO.GetFolder(sLogFolder)
For Each colSubfolder in colFolder.SubFolders
        ' Alt klasör içindeki dosyaları tarıyor
        Set objFolder = objFSO.GetFolder(colSubfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
                ' Dosyanın yaşını hesaplıyor
                iFileAge = now - objFile.DateCreated
                ' Belirlediğiniz gün sınırını aşıyorsa silme işlemi yapılıyor
                if iFileAge > (iMaxAge+1) then
                        objFSO.DeleteFile objFile, True ' Dosya siliniyor
                end if
        Next
Next
2.Next, open the “Task Scheduler” application from the Start menu.

3.Click on “Create Basic Task” to create a new scheduled task.

4.Görevinizi adlandırın ve açıklama ekleyin.

5.Choose “Start a Program” and specify a script or batch file to delete the logs.

6.You’ll see a summary of your task. Click “Finish” or “Create” to create the task.

By following these steps, you can clean up IIS logs at regular intervals on Windows Server. Be cautious when deleting logs and remember to create backups before deleting unnecessary files.


If you have any questions or details you would like to add, feel free to write me.
Categories
Articles SQL Windows

Azure Data Studio or SSMS — which should I use?

Azure Data Studio (ADS) and SQL Server Management Studio (SSMS) are both database management tools used for different purposes, and which tool to use depends on your needs and preferences.

Azure Data Studio (ADS):

Azure Data Studio (ADS):
Azure Data Studio (ADS):
  • ADS offers cross-platform support, meaning it can be used on Windows, macOS, and Linux. This can be essential for collaboration among team members using different operating systems.
  • It is primarily designed for database development and query creation. It is used for editing queries, visualizing query results, and managing databases at a high level.
  • It comes with an integrated query editor with advanced development features such as syntax highlighting, auto-completion, and code hints.
  • ADS can work with multiple database systems (SQL Server, PostgreSQL, MySQL, MongoDB, etc.) and allows you to create different connection profiles.
  • You can extend its functionality using extensions and plugins, enabling you to customize your workflow by installing or developing specific extensions.

SQL Server Management Studio (SSMS):

SQL Server Management Studio (SSMS):
  • SSMS only runs on the Windows operating system and is specifically designed for managing SQL Server. Therefore, it is recommended for those working primarily with SQL Server databases.
  • SSMS allows you to create, edit, manage, and back up database objects. It provides tools for tasks like database backup, security configuration, and performance monitoring.
  • It offers specialized reports and performance monitoring tools for professional SQL Server administration.
  • SSMS provides specialized tools and design surfaces for tasks like creating databases, writing stored procedures, and designing workflows.
  • It offers advanced monitoring and security features for database administrators.

In conclusion, the choice between Azure Data Studio (ADS) and SQL Server Management Studio (SSMS) depends on your project requirements, team member preferences, and the database system you are working with. If you have general database development and query-writing needs across various platforms, ADS might be a better fit. However, if you are primarily working with SQL Server and handling administrative tasks, SSMS is the more suitable choice. To make the best decision for your needs, consider trying out both tools and assessing which one aligns better with your workflow.


If you have any questions or details you would like to add, feel free to write me.

Categories
Articles SQL

What is SQL Server Management Studio (SSMS) and How to Install it?

SQL Server Management Studio (SSMS) is the official tool used for Microsoft SQL Server database management and query operations. SSMS enables you to create, edit, back up, and execute data queries within databases. Additionally, you can configure security settings and monitor database performance in SQL Server.

The core functions of SSMS include:
1.Database Management: SSMS allows you to create, delete, rename, and edit SQL Server databases. It provides graphical interfaces and commands for managing your databases.

2.Query Operations: You can write SQL queries and execute them on SQL Server using SSMS. You can view and process the results, which is used for accessing and manipulating data within your database.

3.Backup and Restore: You can take backups of your databases and restore them when needed. This is essential for preventing data loss and safeguarding your database operations.

4.Security Settings: You can configure security settings for your databases and manage access control. You can define users and roles.

5.Performance Monitoring: It provides tools and reports to monitor the performance of SQL Server. This helps you optimize your database performance.

SQL Server Management Studio is a versatile tool for managing and developing with SQL Server, and it serves as a fundamental component for many database applications that work with SQL Server. By installing SSMS on your computer, you can access the necessary tools to start working with SQL Server. You can easily install SSMS by following the instructions below.


SQL Server Management Studio (SSMS) installation:

1.Downloading SQL Server Management Studio: The first step is to download the latest version of SSMS. You can do this by visiting the official Microsoft website. The download link is as follows:

SQL Server Management Studio Download

SSMS indir
SSMS indir

2.Running the Downloaded File: Double-click the downloaded installation file to run it. If a User Account Control (UAC) window appears, select “Yes” or “Continue” to grant permission.

3.Accepting the License Agreement: Read the license agreement carefully, and if you agree, check the “I accept the license terms” option and click the “Next” button.

4.Installation Path Selection: If you choose the “Custom” installation type, you can select a path in this step to determine where SQL Server Management Studio will be installed. If you prefer to use the default settings, you can click the “Install” button.

install
install
install
install

5.Custom Installation Options: If you have chosen the “Custom” installation option, you can customize which components will be installed in this step. Select the components according to your needs and click the “Next” button.

6.Completion of Installation: The installation process may take some time. Once it’s finished, click the “Close” button to complete the installation.

7.Launching SSMS: When the installation is complete, you can launch the SQL Server Management Studio program using the shortcut on your desktop or from the Start Menu. Start the program, and you can begin managing your SQL Server databases.

You should have successfully installed SSMS on your computer by now. You can use this tool to manage your databases and run queries.


How to Perform Database Management and Performance Monitoring Using SQL Server Management Studio (SSMS):

  1. Connecting to a Server: When SSMS starts, you will need to connect to a server. The “Connect to Server” window will open. This window allows you to choose how to connect to SQL Server.

  • Server Type: Choose the server type. Typically, the “Database Engine” option is used.
  • Server Name: Enter the name or IP address of the SQL Server.
  • Authentication: Choose one of the options, either “Windows Authentication” or “SQL Server Authentication,” to connect to SQL Server.

2.Connecting to the Server: After entering the necessary information, click the “Connect” button. When SSMS successfully connects to the server, it will close the connection window.

3.Viewing Databases: In the “Object Explorer” window on the left side, you can view your existing databases by clicking on the “Databases” button located under the connected server. Here, you will see all the databases, including system databases.

Object Explorer
Object Explorer

4.Creating a New Database: To create a new database, right-click on “Databases” and select “New Database.” In the opened window, you can configure the database’s name, filegroups, and other settings.

New Database
New Database

5.Database Backup and Restore: To back up or restore databases, right-click on “Tasks” and use the “Back Up” or “Restore” options to perform these operations.

Tasks
Tasks

6.Security Settings: To configure the security settings for your databases, go to the “Security” section. Settings related to users, roles, and permissions are managed here.

Security
Security

7.Running Queries: To execute SQL queries, you can open a new query window. You can write and execute SQL queries using the “New Query” option.

New Query
New Query

8.Performance Monitoring: SSMS provides tools and reports for monitoring the performance of SQL Server. This helps you optimize your database’s performance.

Performans Monitor
Performans Monitor

If you have any questions or details you would like to add, feel free to write me.

Categories
Articles SQL

Finding Active and Inactive Databases in SQL Server

To find active and inactive databases in SQL Server when you have a large number of databases, you can use the following code:

CREATE TABLE #T (dbName varchar(100),last_user_seek datetime,last_user_scan datetime,last_user_lookup datetime,last_user_update datetime)
declare @dbId as int
declare @dbname as varchar(100)
declare crs cursor for select dbid,name from sysdatabases 
open crs
fetch next from crs into @dbId,@dbname
while @@FETCH_STATUS=0
begin
Insert Into #T 
Select @dbname,
last_user_seek = MAX(last_user_seek),
last_user_scan = MAX(last_user_scan),
last_user_lookup = MAX(last_user_lookup),
last_user_update = MAX(last_user_update)
From
sys.dm_db_index_usage_stats
WHERE
database_id=@dbId

fetch next from crs into @dbId,@dbname
end 
close crs
deallocate crs 

select * from #t 
drop table #t

Here are the steps to execute this query in SQL Server Management Studio (SSMS):

1.Open SSMS and log in to your SQL Server instance.If you do not have it installed, you can find the article in which I explain its installation here.

2.In SSMS, open a new query window by clicking “New Query.”

3.Paste the provided query into the query window.

4.Execute the query by clicking the “Execute” button or pressing F5.

After executing the query, you will see a result similar to the one you mentioned, which shows when each database had its last user seek, scan, lookup, and update operations.


For those who are curious, here’s an explanation of the query:

  1. The “CREATE TABLE #T” command creates a temporary table. This temporary table is named #T and contains several columns: dbName, last_user_seek, last_user_scan, last_user_lookup, and last_user_update. This table is used to store the results.
  2. The “declare @dbId as int” and “declare @dbname as varchar(100)” commands declare two variables to be used in the query. @dbId is an integer (int) variable, and @dbname is a character string (varchar) variable.
  3. The “declare crs cursor for select dbid,name from sysdatabases” command defines a cursor. It selects database IDs (dbid) and names (name) from the sysdatabases system view, creating a cursor that will allow us to loop through all the databases.
  4. The “open crs” command opens the defined cursor and prepares it to start processing database information.
  5. The “fetch next from crs into @dbId,@dbname” command moves the cursor to the next database record and assigns the values of @dbId and @dbname variables to the current record. This process is repeated as the cursor iterates through all the databases.
  6. The “while @@FETCH_STATUS=0” command checks whether the cursor has reached the end. As long as the @@FETCH_STATUS variable is 0, meaning the cursor has found more database records, the loop continues.
  7. Inside the loop, the “Insert Into #T” command adds data to the #T temporary table. This command retrieves index usage statistics for a specific database from the sys.dm_db_index_usage_stats system view and inserts the maximum values of these statistics into the #T table. This way, the latest index usage statistics for each database are consolidated in this table.
  8. The loop uses the “fetch next from crs into @dbId,@dbname” command to move to the next database record, and the process is repeated for other databases.
  9. When all databases have been traversed, the “close crs” command closes the cursor, and the “deallocate crs” command cleans up the cursor.
  10. The “select * from #t” command displays the results from the #T table. These results include the latest index usage statistics for each database.
  11. Finally, the “drop table #t” command deletes the temporary table because the results are no longer needed.

kaynak : https://omercolakoglu.net/


If you have any questions or details you would like to add, feel free to write me.

Categories
Articles SQL

Index Maintenance in SQL Server: Determining REORGANIZE and REBUILD Operations

The concept of using SQL queries to determine and rectify the fragmentation rates of indexes is quite important. This query serves as a useful tool to understand how indexes require maintenance for improving the performance of databases. I will explain the process of using the provided SQL query to check index fragmentation rates and perform necessary corrections when needed.

SQL Query:

SELECT S.name as 'Schema',
T.name as 'Table',
I.name as 'Index',
DDIPS.avg_fragmentation_in_percent,
DDIPS.page_count
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS DDIPS
INNER JOIN sys.tables T on T.object_id = DDIPS.object_id
INNER JOIN sys.schemas S on T.schema_id = S.schema_id
INNER JOIN sys.indexes I ON I.object_id = DDIPS.object_id
AND DDIPS.index_id = I.index_id
WHERE DDIPS.database_id = DB_ID()
and I.name is not null
AND DDIPS.avg_fragmentation_in_percent > 0
ORDER BY DDIPS.avg_fragmentation_in_percent desc

This SQL query will list the fragmentation rates and page counts of indexes in the current database. Understanding the extent of index fragmentation is crucial because it helps us determine how to rectify them.

Subsequently, the results of this query can be copied into an Excel table, and the following Excel formula can be used to automatically generate index correction queries:

=EĞER(D2>10;BİRLEŞTİR("ALTER INDEX ";C2;" ON ";B2;" ";EĞER(D2<30;"REORGANIZE";"REBUILD");" WITH(ONLINE=ON)";"");"")

This Excel formula automatically generates ALTER INDEX queries based on the index fragmentation percentage. If the index fragmentation percentage is greater than 10 and less than 30, the REORGANIZE operation is applied; otherwise, the REBUILD operation is applied.

This concept in the article will assist database administrators in automating index maintenance in SQL Server or similar database systems, thereby enhancing database performance. Regular index maintenance is a critical step in optimizing database performance.


How REORGANIZE and REBUILD Operations are Determined in SQL:

In SQL Server, the “REORGANIZE” and “REBUILD” operations for index maintenance are determined based on the degree of index fragmentation and the needs of the database. Here are the factors that influence this decision:

1. Index Fragmentation Rate (avg_fragmentation_in_percent): The fragmentation rate of indexes is a critical metric that indicates how fragmented an index is. The fragmentation rate is automatically calculated by SQL Server and can be obtained using the `sys.dm_db_index_physical_stats` function. Typically, it is determined using the following rules:

  • If the fragmentation rate is between 0–10%, the index is considered normal.
  • If the fragmentation rate is between 10–30%, a “REORGANIZE” operation may be applied.
  • If the fragmentation rate is above 30%, a “REBUILD” operation may be required.
  1. These operations are designed to optimize index performance and, ultimately, the performance of the database. The choice between “REORGANIZE” and “REBUILD” depends on the specific fragmentation level and the database’s performance requirements.
  2. “Database Load and Usage Patterns: The load and usage patterns of a database also influence index maintenance. If a database experiences heavy write operations or a high volume of transactions, caution should be exercised during maintenance. In such cases, the ‘REORGANIZE’ operation may be preferred because it is a lighter process. The ‘REBUILD’ operation is more aggressive and resource-intensive, necessitating careful planning.
  3. Database Size and Resources: The size of the database and the available resources impact maintenance decisions. The ‘REBUILD’ operation requires more resources, potentially leading to longer execution times, which can have a greater impact on your resources in the case of large databases. For smaller databases, the ‘REBUILD’ operation may pose fewer concerns.
  4. Database Criticality: The criticality of the database determines the allocation of time and resources for maintenance tasks. Maintenance operations for critical databases should be planned more meticulously.
  5. Operating System and Hardware: The allocation of operating system and hardware resources to the database is also crucial. If you have more powerful hardware, you may opt for the ‘REBUILD’ operation more frequently.

These are fundamental factors to consider when determining the ‘REORGANIZE’ and ‘REBUILD’ operations for index maintenance. Every database is unique and may have specific requirements, so maintenance operations should be tailored to the needs of your database. Ideally, index fragmentation rates should be monitored at regular intervals, and maintenance operations should be planned in alignment with your database management policies.”


Here’s the explanation of the SQL code:

1.SELECT Statement: This statement specifies which columns will be included in the result set. Below are the names of the columns to be used, along with some of them having alias names:

  • S.name as 'Schema': A column representing the schema name, with the alias name ‘Schema’.
  • T.name as 'Table': A column representing the table name, with the alias name ‘Table’.
  • I.name as 'Index': A column representing the index name, with the alias name ‘Index’.
  • DDIPS.avg_fragmentation_in_percent: A column representing the average fragmentation percentage.
  • DDIPS.page_count: A column representing the page count.

2.FROM Clause: This clause specifies the source from which data will be retrieved. In this query, the sys.dm_db_index_physical_stats function is used to obtain the physical statistics of the indexes in the database.

  • sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS DDIPS: This function returns the physical statistics of the indexes in the database. DB_ID() retrieves the identity of the current database. AS DDIPS is an alias given to this result set.

3.INNER JOIN Statements: These statements are used to join the data. In the query, the results of sys.tablessys.schemas, and sys.indexes tables are combined with the results of sys.dm_db_index_physical_stats. This allows the relevant table, schema, and index information to be brought together.

4.WHERE Clause: This clause is used to filter the query results. In this query, only indexes that meet specific conditions are included. For example, it selects indexes where the fragmentation rate is greater than 0 and where the index is not null.-

5.ORDER BY Clause: This clause determines how the results are sorted. The query results are ordered from highest to lowest based on the fragmentation percentage, meaning the most fragmented indexes are listed first.


If you have any questions or details you would like to add, feel free to write me.