PDA

View Full Version : زیاد شدن حجم دیتابیس



negar.rafie
چهارشنبه 11 مرداد 1391, 13:27 عصر
سلام
فرض کنید ما در دیتابیس عمل حذف و درج را زیاد انجام می دهیم و مثلا عکس هم در دیتابیس ذخیره می کنیم
حالا اگر مثلا 10رکورد به دیتابیس اضافه کنیم حجم دیتابیس می شود 1 مگابایت
حالا مشکل اینجاست که اگر این رکورد ها را حذف کنیم باز حجم دیتابیس 1 مگابایت باقی می ماند
در اکسس برای حل ایم مشکل از قسمت Database Tools روی گزینه compact and repair database کلیک می کنیم

90595

حالا فرض کنید دیتابیس ما sql server است
حالا چه کدی در برنامه خود بنویسیم که مثلا اگر کاربر روی یک دکمه کلیک کرد این عملیات انجام پذیرد

barnamenevisforme
چهارشنبه 11 مرداد 1391, 13:50 عصر
سلام روی database راست کلیک کنید و از tab مربوط به option استفاده کنید و گزینه auto shrink رو انتخاب کنید.

negar.rafie
چهارشنبه 11 مرداد 1391, 14:00 عصر
یعنی اگر این گزینه را True کنم به صورت اتوماتیک عمل compact را انجام میده؟
حالا اگر این طور باشد هر چند وقت یکبار این کار را انجام میدهد؟

barnamenevisforme
چهارشنبه 11 مرداد 1391, 14:24 عصر
طبق چیزی که توی books online گفته خود sql server به صورت دوره ای (اشاره به زمان خاصی نکرده) در صورت فعال بودن این گزینه واسه یه database این کار رو انجام میده.و از طرفی امکان انجام این کار به صورت دستی یا manualهم هست و حتی میشه واسه اون زمان خاصی رو تعریف کرد که مثلا هر روز و یا هر هفته یک بار این کار رو انجام بده(این کار از طریق رسات کلیک رو databaseو انتخاب گزینه shrink از منوی all task امکان پذیره،واسه تعریف دوره هم از گزینه schedule استفاده کنید.

Shrinking Databases

SQL Server 2000 autoshrinks databases that have a large amount of free space. Only those databases where the autoshrink option has been set to true are candidates for this process. The server checks the space usage in each database periodically. If a database is found with a lot of empty space and it has the autoshrink option set to true, SQL Server reduces the size of the files in the database. You can also use SQL Server Enterprise Manager or the DBCC SHRINKDATABASE and DBCC SHRINKFILE statements to shrink the files of a database manually.
Files are always shrunk from the end. For example, if you have a 5 GB file and specify 4GB as the target_size in a DBCC SHRINKDB statement, SQL Server will free as much space as it can from the last 1 GB of the file. If there are used pages in the part of the file being released, SQL Server first relocates the pages to the part being retained. You can only shrink a database to the point where it has no free space remaining. For example, if a 5GB database has 4 GB of data and you specify 3 GB as the target_size of a DBCC SHRINKDATABASE statement, only 1 GB will be freed.
If a DBCC SHRINKDATABASE or DBCC SHRINKFILE statement cannot reclaim all the space in a log file, the statement will issue an informational message indicating what action you must perform to make more space eligible to be freed. For more information about shrinking log files, see Shrinking the Transaction Log (http://barnamenevis.org/8_ar_da2_1uzr.htm).

negar.rafie
چهارشنبه 11 مرداد 1391, 14:45 عصر
حالا چه طوری با کد نویسی این کار را انجام بدیم؟
به طوری که با کلیک روی یک دکمه عمل compact صورت پذیرد؟

barnamenevisforme
چهارشنبه 11 مرداد 1391, 16:18 عصر
http://www.codeproject.com/Tips/132106/Clearing-Transaction-Logs?cmt=30603#cmt2_132106
http://www.codeproject.com/Tips/142314/How-to-Shrink-SQL-Server-database-files
امیدوارم مفید واقع بشن.

hakim22
چهارشنبه 11 مرداد 1391, 16:35 عصر
برای اینکار شما Refrence از SQL Server رو به برنامه اضافه می کنید و از Sql.management و SMO می توانید تمام محیط برنامه SQL server را از طریق کد انجام دهید یا یک Stored procedure بسازی و اونو run کنید. لازمه با T-SQL آشنایی داشته باشید.