Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Is a Trigger appropriate for the following scenario, or do I need to try something else?

Posted on 2011-03-18
5
Medium Priority
?
234 Views
Last Modified: 2012-05-11
We have a situation developing where we need to make use of several non-clustered indexes on several key tables in order to generate reports in a reasonable time frame.  The problem is these are appended bulk-transaction databases which get updated with new records weekly.  There is one data set for each client and we have several, often two to three per server.

Is it effective to use a Trigger to disable and then rebuild the indexes before and then after the INSERT, UPDATE, or DELETE statements are completed?

I may be misunderstanding the principle of a trigger - does the insertion of each row trigger the trigger? Or does it trigger once during the opening of the operation, and then close when the operation completes?
0
Comment
Question by:propharma
5 Comments
 
LVL 11

Expert Comment

by:JoeNuvo
ID: 35170311
the trigger will be fired for each query.

if the query effect only 1 row of the table with trigger, there will be 1 fire of trigger with that 1 row
if the query effect 100 rows of that table, there will be 1 fire of trigger with 100 rows
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 2000 total points
ID: 35171358
just on general principles what you are suggesting is not a good idea...
and i would also suggest that it could not be guaranteed to function in future even if it appeared to work now...

how would the dbms work... if you dropped the index it was attempting to use to perform its access with?

the triggers fires each time a "set of rows" is affected....

which depends on the style of the originating sql  and how and where that is processed in the processing... remember that a table can have many triggers specified on it and you have little control over the order in which they are processed... also triggers can be "nested" and called recursively...

a better process is to code the drop and recreate of any indexes around the statement/process you are running... (e.g. a prior and succeeding job steps in a batch job...)  that would also lead to a simpler test and maintainence process.
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 35171611
It depends on the existence of your define triggers type. if they are of type 'for insert' it should be run when  data inserted into table so if you need to disable trigger then first disable trigger and then insert data.
0
 

Author Comment

by:propharma
ID: 35173034
So perhaps a stored proceedure that can be executed from any DB then?
0
 

Author Closing Comment

by:propharma
ID: 35487842
Very good explanation of the problem as well as an alternate solution to the core issue.  Thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question