简体   繁体   中英

Procedure Takes Too Long To Execute

I am using a stored procedure to fetch the data. I am using multiple joins and inner queries in it that is the reason to make it too slow. Is there any alternative to avoid the inner query for master detail table?

Actually earlier I was using a LINQ query to fetch the data but it also was taking too long to execute then I choose the stored procedure but it also taking the same time to execute. So how could I make it faster?

Here is my stored procedure:

ALTER PROCEDURE USP_GetUserDetailByID 
    @id INT
AS
BEGIN
    SELECT TOP 1
        uinfo.ID ID,
        uinfo.UniqueUserID UniqueUserID,
        uinfo.ID UserInfoID,
        uinfo.ReferredID ReferredID,
        uinfo.UserTypeID UserTypeID,
        uinfo.GroomBrideName GroomBrideName,
        uinfo.EmailID EmailID,
        uinfo.[Password] [Password], 
        uinfo.ProfileCreatedFor ProfileCreatedFor,
        uinfo.DateOfBirth DateOfBirth,
        uinfo.Gender Gender,
        uinfo.Age Age,
        uinfo.Height Height,
        uinfo.City City,
        uinfo.Country Country,
        uinfo.Phone Phone,
        uinfo.Mobile Mobile,
        uinfo.RegisteredMobileNumber RegisteredMobileNumber,
        uinfo.About About,
        uinfo.[Status] [Status],
        rel.Religion Religion,
        rel.MotherTongue MotherTongue,
        rel.Section Section,
        rel.Division Division,
        edu.EducationLevel EducationalLevel,
        edu.EducationFeild EducationalField,
        edu.GraduationDegree GraduationDegree,
        edu.MasterDegree MasterDegree,
        edu.WorkWith WorkWith,
        edu.WorkingGroup WorkingGroup,
        edu.WorkingAs WorkingAs,
        edu.AnnualIncome AnnualIncome,
        edu.InterestedInSettlingAbroad InsertedInSettingAbroad,
        hob.Hobbies Hobbies,
        hob.DressStyle,
        hob.SportsFitness SportsFitness,
        hob.FavouriteCuisine FavouriteCuisine,
        hob.FavouriteVacationDestination FavouriteVacationDestination,
        hob.FavouriteMusic FavouriteMusic,
        hob.FavouriteBooks FavouriteBooks,
        hob.PreferredMovies PreferredMovies,
        life.Diet Diet,
        life.Smoke Smoke,
        life.Drink Drink,
        life.SkinTone Complexion,
        life.BodyType BodyType,
        life.BloodGroup BloodGroup,
        life.[Weight] [Weight],
        life.ResidentialStatus ResidentialStatus,
        life.OwnHouse OwnHouse,
        life.OwnCar OwnCar,
        life.LanguageKnown LanguageKnown,
        fd.FamilyValue FamilyValue,
        (SELECT DetailData FROM MasterDetails WHERE ID = fd.FamilyValue) FamilyValueText,
        fd.FamilyType FamilyType,
        (SELECT DetailData FROM MasterDetails WHERE ID = fd.FamilyType) FamilyTypeText,
        fd.FamilyStatus FamilyStatus,
        (SELECT DetailData FROM MasterDetails WHERE ID = fd.FamilyStatus) FamilyStatusText,
        fd.Father Father,
        (SELECT DetailData FROM MasterDetails WHERE ID = fd.Father) FatherText,
        fd.Mother Mother,
        (SELECT DetailData FROM MasterDetails WHERE ID = fd.Mother) MotherText,
        fd.Brothers Brothers,
        fd.MarriedBrothers MarriedBrothers,
        fd.Sisters Sisters,
        fd.MarriedSisters MarriedSisters,
        fd.LivingWithParents LivingWithParents,
        rel.ReligiousValues ReligiousValues,
        uinfo.HightLightUserAbout HightLightUserAbout,
        uinfo.FbUserDetail FbUserDetail,
        (SELECT DetailData FROM  MasterDetails WHERE ID = uinfo.ProfileCreatedFor) ProfileCreatedText,
        (SELECT DetailData FROM  MasterDetails WHERE ID = uinfo.Height) HeightText,
        (SELECT ID FROM MasterDetails WHERE ID =  uinfo.MaritalStatus) MaritalStatus,
        (SELECT DetailData FROM  MasterDetails WHERE ID = uinfo.MaritalStatus) MaritalStatusText,
        (SELECT ID FROM MasterDetails WHERE ID = uinfo.[State]) [State],
        CASE WHEN uinfo.City IS NULL THEN '' ELSE (SELECT DetailData FROM MasterDetails WHERE ID = uinfo.City) END as CityText,
        CASE WHEN uinfo.[State] IS NULL THEN '' ELSE (SELECT DetailData FROM MasterDetails WHERE ID = uinfo.[State]) END as StateText,
        (SELECT MasterCategoryID FROM MasterDetails WHERE ID = uinfo.Country) CountryId,
        (SELECT DetailData FROM MasterDetails WHERE ID = uinfo.Country) CountryText,
        (SELECT DetailData FROM MasterDetails WHERE ID = rel.MotherTongue) MotherTongueText,
        (SELECT DetailData FROM MasterDetails WHERE ID = rel.Section) SectionText,
        (SELECT DetailData FROM MasterDetails WHERE ID = rel.Division) DivisonText,
        (SELECT DetailData FROM MasterDetails WHERE ID = edu.EducationLevel) EducationalLevelText,
        (SELECT DetailData FROM MasterDetails WHERE ID = edu.EducationFeild) EducationalFeildText,
        (SELECT DetailData FROM MasterDetails WHERE ID = edu.GraduationDegree) GraduationDegreeText,
        (SELECT DetailData FROM MasterDetails WHERE ID = edu.MasterDegree) MasterDegreeText,
        (SELECT DetailData FROM MasterDetails WHERE ID = edu.WorkWith) WOrkingWithText,
        (SELECT DetailData FROM MasterDetails WHERE ID = edu.WorkingGroup) WorkingGroupText,
        (SELECT DetailData FROM MasterDetails WHERE ID = edu.WorkingAs) WorkingAsText,
        (SELECT DetailData FROM MasterDetails WHERE ID = edu.AnnualIncome) AnnualIncomeText,
        (SELECT DetailData FROM MasterDetails WHERE ID = hob.Hobbies) HobbiesText,
        (SELECT DetailData FROM MasterDetails WHERE ID = hob.DressStyle) DressStyleText,
        (SELECT DetailData FROM MasterDetails WHERE ID = hob.SportsFitness) SportsFitnessText,
        (SELECT DetailData FROM MasterDetails WHERE ID = hob.FavouriteCuisine) FavouriteCuisineText,
        (SELECT DetailData FROM MasterDetails WHERE ID = hob.FavouriteMusic) FavouriteMusicText,
        (SELECT DetailData FROM MasterDetails WHERE ID = hob.PreferredMovies) PreferredMoviesText,
        (SELECT DetailData FROM MasterDetails WHERE ID = life.Diet) DietText,
        (SELECT DetailData FROM MasterDetails WHERE ID = life.SkinTone) ComplexionText,
        (SELECT DetailData FROM MasterDetails WHERE ID = life.BodyType) BodyTypeText
    FROM
        UsersInfo uinfo 
    LEFT JOIN 
        EducationAndOccupation edu on uinfo.ID = edu.UserID 
    LEFT JOIN 
        ReligionAndEthinicity rel on uinfo.ID = rel.UserID
    LEFT JOIN 
        HobbiesAndInterests hob on uinfo.ID = hob.UserID
    LEFT JOIN 
        LifestyleAndAttributes life on uinfo.ID = life.UserID
    LEFT JOIN 
        FamilyDetails fd on uinfo.ID = fd.UserID
    WHERE
        uinfo.ID = @id
END

The first thing you should do is run that statement through EXPLAIN to see what your query is doing.

In SSMS, make sure you have the database selected and open a new query window. Paste your SQL into the window, then choose Query -> display estimated execution plan (Ctrl + L)

The query plan is displayed in the window at the bottom. Look at the plan, it will tell you where things are taking time (percentage relative to batch) If you are unfamiliar with plans, have a look here...

http://www.simple-talk.com/sql/performance/execution-plan-basics/[ ^]

You could also run your query through the tuning advisor to see if any indexes are recommended. However, if table titlefee is the table with the most records, you need to consider the operations you are performing against it.

1) You are limiting the results from this table by field dtIssue. Is this field part of an index, or is it causing a table scan (look in your execution plan)

2) You are summarising by field curAmount - this could possibly be indexed.

Using the execution plan + query tuning advisor, you should be able to index your tables for optimal performance.

However, by introducing indexes, you have to weigh that up against how often the table is updated \\ the process that updates the data (eg user application? overnight ETL?). It can be a bit of a dark art to get the correct balance of READ against WRITE performance :)

Reference : How-to-make-fast-Stored-Procedure

尝试为您的查询执行计划,这将使您花费更多的成本,并且在需要时还会建议索引。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM