Stored procedure or trigger | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Stored procedure or trigger

Hi all,<br /><br />I hesitate what to use SPs or triggers. I would like my queries to be as fast as possible. My data base is something like this:<br /><br />UsersTable<br />————<br />UserID(PK) | UserName | Password | Name | Addr | E-mail | UsrType | Role | LastLogOn<br /><br />EstatesTbale<br />———–<br />EstateID(PK) City Phone Heating Furniture TypeEstate(FK) TypeOffr(FK) TypeConstr(FK)<br /><br />OfferTable<br />————<br />EstateID(FK) UserID(FK) Value ParamID(FK)<br /><br />Param Table<br />————–<br />ParamID(PK) Param<br /><br />TypeEstateTable<br />—————-<br />Unikey(PK) TypeEstate<br /><br />TypeOFFRTable<br />————-<br />Unikey(PK) TypeOffr<br /><br />TypeConstr<br />————-<br />Unikey(PK) TypeConstr<br /><br />PictureEstateTable<br />———————<br />ID(PK) EstateID(FK) PictureFilePath<br /><br />DescriptionEstateTable<br />————————-<br />ID(PK) EstateID(FK) TextDescription<br /><br /><br />I develop Web based application using ASP.NET/C#/MS SQL. I’d like to ask you would you change something in the tables? And also, I would like what would be better:<br />1. To import user data by registration whit SP or Trigger?<br />2. When a registrated user would like to place an Advertisement what would be better again stored procedure or trigger?<br />For this inserting I wrote my first SP:<br /><br />CREATE PROCEDURE prSVOFFRDATA<br />@CITY VARCHAR(20),<br />@PHONE CHAR(1), — Insert ‘Y’ or ‘N'<br />@FURN CHAR(1), — Insert ‘Y’ or ‘N'<br />@HEATING CHAR(1), — Insert ‘Y’ or ‘N'<br />@TYPEESTATE CHAR(3),<br />@TYPEOFFR INT,<br />@TYPECONSTR CHAR(1),<br />@USERID INT,<br />@ValuePrice BIGINT,<br />@ValueFloor BIGINT,<br />@ValueArea BIGINT,<br />@ValueRooms BIGINT,<br />@PICTURE VARCHAR(50),<br />@DESCRIPTION VARCHAR(255)<br /><br />AS<br />INSERT INTO BLEK.EstatesTable(City,Phone,Furn,Heating,TypeEstate,TypeOffr,TypeConstr)<br />VALUES(@CITY,@PHONE,@FURN,@HEATING,@TYPEESTATE,@TYPEOFFR,@TYPECONSTR)<br />DECLARE @IDENT INT<br />SELECT @IDENT = SCOPE_IDENTITY()<br />INSERT INTO BLEK.OFFERTABLE(EstateID,UserID,Value,ParamID)<br />VALUES(@IDENT,@USERID,@ValuePrice,’price’)<br />INSERT INTO BLEK.OFFERTABLE(EstateID,UserID,Value,ParamID)<br />VALUES(@IDENT,@USERID,@ValueFloor,’floor’)<br />INSERT INTO BLEK.OFFERTABLE(EstateID,UserID,Value,ParamID)<br />VALUES(@IDENT,@USERID,@ValueArea,’area’)<br />INSERT INTO BLEK.OFFERTABLE(EstateID,UserID,Value,ParamID)<br />VALUES(@IDENT,@USERID,@ValueRooms,’room’)<br /><br />IF (@PICTURE IS NOT NULL)<br />INSERT INTO BLEK.PICTUREESTATETABLE(EstateID,PICTUREFILEPATH) VALUES(@IDENT,@PICTURE)<br />IF (@DESCRIPTION IS NOT NULL)<br />INSERT INTO BLEK.DSCRIPTIONESTATETABLE(EstateID,TextDescription) VALUES(@IDENT,@DESCRIPTION)<br /><br />You can see that it is my first SP<img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ /> I dont know how to optimize it. I don’t have an idea would be better if I CREATE VIEW and use TRIGGER to populate data in the tables. I have to UPDATE this rows when a user wants to change the data in his/her advertisement.<br />I have also a big SEARCH query between 4 tables. What would you mean? To CREATE VIEW and to use SELCT QUERY from this VIEW or to create Stored PRocedure.<br />Could help me please, could you give me an advice?<br /><br /><br /><br />Thank you very much!
Hi ya, This is always very difficult to answer online as there would be a thousand and one questions to work out which might be the most optimal… First question is why do you have so many tables, i.e. why not have price, floor, area, room, picture and description in your main table? second question, it sounds like the main performance is going to be in the searching aspect, so what are the searches that you are going to need to support? This may change the table design too whether a view or sp is faster for a select will depend on the complexity of the sql. I’d suggest try both and use Profiler to check which one is faster. A view allows you to have lots of different search conditions, where as a procedure would be quicker if you have specific predefined searches that you have to be able to do inserting using sp is likely to be better performing than a view with ‘instead of triggers’ Sorry not many answers… Cheers