Showing posts with label reporting. Show all posts
Showing posts with label reporting. Show all posts

Thursday, March 22, 2012

Conversion from Crystal Report

Hi all,
I am in the process of converting some reports from Crystal Reports to
Reporting Services.
I want to examine the value of a field in the next row. Crystal reports
provides the following functions for that purpose NextIsNull(), NextValue().
Is any equivalent method in Reporting Services?
Thanks
SamUnfortunately, no. We provide a Previous() function but not Next().
--
Brian Welcker
Group Program Manager
SQL Server Reporting Services
This posting is provided "AS IS" with no warranties, and confers no rights.
"Samuel" <samuel@.photoninfotech.com> wrote in message
news:udsDvkMYEHA.2672@.tk2msftngp13.phx.gbl...
> Hi all,
> I am in the process of converting some reports from Crystal Reports to
> Reporting Services.
> I want to examine the value of a field in the next row. Crystal reports
> provides the following functions for that purpose NextIsNull(),
> NextValue().
> Is any equivalent method in Reporting Services?
> Thanks
> Sam
>

Monday, March 19, 2012

Controls on reports

Hi,
I have just created my first report using VS.NET 2005 and Reporting
Services. There is a small choice of controls to une on my reports.
The simplest control is Label, which I cannot find.
Please tell me how to place some text on a report.
regards
PrzemoThere is no difference between text and label. The reason is, this is a
report not a data entry form so it is not necessary to differentiate between
them. Put a text box on the form and then just type in whatever text you
want. Or, if you are going to be more complicated, set it to an expression
and use the expression builder to put in more complex text (for instance you
can refer to the parameters so you can show what parameters the user picked
when running the report).
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Przemo" <Przemo@.discussions.microsoft.com> wrote in message
news:C75CC836-E033-4ACD-9FE1-35992B25B326@.microsoft.com...
> Hi,
> I have just created my first report using VS.NET 2005 and Reporting
> Services. There is a small choice of controls to une on my reports.
> The simplest control is Label, which I cannot find.
> Please tell me how to place some text on a report.
> regards
> Przemo

Controlling the language returned from Cubes

Hi!
Hopefully someone can help me with this, because I am really stuck.
I am reporting from a cube whose members have captions defined for
multiple languages. I would like to control the language displayed using a
report parameter. These reports will be rendered using Web Services.
Is there a way I can set the language setting of the query dynamically?
Setting the locale for the report did not seem to make any difference :>(.
Any help would be greatly appreciated.
Thanks,
BobBob Hug wrote:
> Hi!
> Hopefully someone can help me with this, because I am really
> stuck. I am reporting from a cube whose members have captions
> defined for multiple languages. I would like to control the language
> displayed using a report parameter. These reports will be rendered
> using Web Services. Is there a way I can set the language setting
> of the query dynamically? Setting the locale for the report did not
> seem to make any difference :>(.
> Any help would be greatly appreciated.
> Thanks,
> Bob
Could the rs:ParameterLanguage be a help for you?
http://download.microsoft.com/download/7/f/b/7fb1a251-13ad-404c-a034-10d79ddaa510/SP1Readme_EN.htm
roland|||Can't you just ask for the appropriate member caption in whatever language
you want?
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/olapdmad/agmemberprops_8ier.asp.
--
Brian Welcker
Group Program Manager
SQL Server Reporting Services
This posting is provided "AS IS" with no warranties, and confers no rights.
"Bob Hug" <rhug@.brassring.com> wrote in message
news:uoIhIAVXEHA.3988@.tk2msftngp13.phx.gbl...
> Hi!
> Hopefully someone can help me with this, because I am really stuck.
> I am reporting from a cube whose members have captions defined for
> multiple languages. I would like to control the language displayed using a
> report parameter. These reports will be rendered using Web Services.
> Is there a way I can set the language setting of the query dynamically?
> Setting the locale for the report did not seem to make any difference :>(.
> Any help would be greatly appreciated.
> Thanks,
> Bob
>|||Brian,
Thanks for the response.
The cube is configured exactly as your reference
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/olapdmad/agmemberprops_8ier.asp
describes. My problem is how to control the language that is returned in the
report. I believe that the cube is correctly configured. Analysis Manager
and OWC both display the expected language based upon the language setting
of the client. Reporting Services' reports return the expected language when
I set the locale of the Reporting Server. The locale of the Reporting Server
seems to determine the language that the cube returns, perhaps because this
is determining the language of the connection? When I change the Report
Server's locale to French, for example, I get French returned like I expect.
Perhaps this simply a reflection of my lack of MDX knowledge, but I have not
been able to find a way to specify in the query the language I want in
effect. There does not seem to be anything like 'Set Language'.
Any ideas?
Thanks,
Bob
"Brian Welcker [MSFT]" <bwelcker@.online.microsoft.com> wrote in message
news:e1uhcOaXEHA.1888@.TK2MSFTNGP11.phx.gbl...
> Can't you just ask for the appropriate member caption in whatever language
> you want?
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/olapdmad/agmemberprops_8ier.asp.
> --
> Brian Welcker
> Group Program Manager
> SQL Server Reporting Services
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> "Bob Hug" <rhug@.brassring.com> wrote in message
> news:uoIhIAVXEHA.3988@.tk2msftngp13.phx.gbl...
> > Hi!
> > Hopefully someone can help me with this, because I am really stuck.
> > I am reporting from a cube whose members have captions defined for
> > multiple languages. I would like to control the language displayed using
a
> > report parameter. These reports will be rendered using Web Services.
> > Is there a way I can set the language setting of the query
dynamically?
> > Setting the locale for the report did not seem to make any difference
:>(.
> >
> > Any help would be greatly appreciated.
> >
> > Thanks,
> > Bob
> >
> >
>

Controlling the freeze pane location in Reporting Services

So I have a report that has a header, and in the body there is a
table. The table has a heading row. When I export this report to
excel, the freeze pane is put under the header.
Is there a way to specify the location of the freeze pane when the
report is exported to excel? I would like to have the freeze pane
directly below the column header row.
One possible solution I thought of was to put the column header row
into the actual header, but I can't do that as right above the column
header row is a summary of the data in the table. This summary
references fields from my datasets. And anything that references a
field cannot be put in the header row.
Thanks in advance.On Apr 28, 8:41 am, Jesse...@.gmail.com wrote:
> So I have a report that has a header, and in the body there is a
> table. The table has a heading row. When I export this report to
> excel, the freeze pane is put under the header.
> Is there a way to specify the location of the freeze pane when the
> report is exported to excel? I would like to have the freeze pane
> directly below the column header row.
> One possible solution I thought of was to put the column header row
> into the actual header, but I can't do that as right above the column
> header row is a summary of the data in the table. This summary
> references fields from my datasets. And anything that references a
> field cannot be put in the header row.
> Thanks in advance.
As far as I know, there is not really anyway to control this. I'm
actually surprised that you have managed to get the Excel export to
maintain the freeze panes at all, as I have not seen it work
automatically after export. Sorry that I could not be of further
assistance.
Regards,
Enrique Martinez
Sr. Software Consultant

Controlling Security through Web Application

Hello all,

I have some questions hopefully you can help with regarding controlling access through ASP.NET.

We'd like to take advantage of reporting services functionality for reports, but we'd like to use our own security model. We have an extensive database-driven security model that exists independently of active directory and windows permissions. We use the NT Logon of the user, but that is all we use. Everything else is maintained within our database structure. We also have all of our web pages and reports in a large database with individual ID's that are permissioned against th

Currently we need a way to link users to reporting services using our own authentication, but this presents a problem. Currently we have a wrapper page (let's call it ReportAccess.aspx). That page authenticates the user and decides whether or not they have access to the report, and then should deliver the report.

Here is where I'm not sure what to do. Our original component simply threw the report URL into an IFRAME. So in order to make this work, we had to give ALL users permissions to all reports on reporting services, since the credentials get passed through. The problem here is that savvy people could look at the URLs and hack their way into reports they should not be seeing.

Ideally, we'd like to only give access to one account and have the ReportAccess.aspx page control that access, but I am not sure how to pull this off. Is this even possible?

The ugly alternative would be maintaining permissions in our system AND Reporting Services ... which would be a lot of work and juggling. There has to be a better way

Okay, I came across the ReportViewer control in ASP.NET and that may be exactly what I was looking for, but I am having a little bit of trouble getting it to work.

I have setup the component as follows:

<rsweb:ReportViewer ID="ReportViewer1" runat="server" ProcessingMode="Remote" >
<ServerReport ReportServerUrl="http://rs2k5/Reports/Pages/Folder.aspx" ReportPath="/MyReportFolder/MyReportName" DisplayName="Test Report" />
<LocalReport />
</rsweb:ReportViewer>

For the record, you can access the reports using this URL:
http://rs2k5/Reports/Pages/Report.aspx?ItemPath=%2fMyReportFolder%2fMyReportName

When I use [http://rs2k5/Reports/Pages/] as the ServerURL, I get a 404 file not found. Interestingly enough, when I use [http://rs2k5/Reports/Pages/Folder.aspx], I get something back, but it looks like this:

  • Client found response content type of 'text/html; charset=utf-8', but expected 'text/xml'. The request failed with the error message: -- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <script language="JScript" type="text/Javascript" ****snipped - very long HTML here****
    |||To get entirely around security using the report viewer, you need to use an rdlc local to your project that does not rely on the report server all.|||

    This is a pretty interesting approach. I had no idea the RDLC option existed. I'm going to test some with that.

    Currently, I was able to get the ReportViewer working with Remote option. Turns out you can't point at your ReportServer, instead you have to point at your ReportManager. Using impersonation, I was able to successfully mimic a user's security, so that all users could access the reports using one single account (and then I can use my own validation on the ASP.NET page.

  • Controlling Reporting Services Export

    Hello,
    I have a CSV export that needs some fields qualified by double quotes and
    some that do not. How do I control this behavior in Reporting Services?
    I have tried specifying a blank text qualifier in rsreportserver.config and
    surrounding the applicable fields by double quotes but that doesn't work as
    the double quote qualifier gets repeated.
    TIA,
    Ray
    SS2K5On Jan 7, 4:06 pm, raybouk <rayb...@.discussions.microsoft.com> wrote:
    > Hello,
    > I have a CSV export that needs some fields qualified by double quotes and
    > some that do not. How do I control this behavior in Reporting Services?
    > I have tried specifying a blank text qualifier in rsreportserver.config and
    > surrounding the applicable fields by double quotes but that doesn't work as
    > the double quote qualifier gets repeated.
    > TIA,
    > Ray
    > SS2K5
    The quickest way to accommodate this is to use casting in SSRS (i.e.,
    CStr(Fields!SomeFieldName.Value)). You would cast the fields that you
    need to have quotes around. Also, you could use the format part of the
    Properties tab for the fields you need to have the quotes around. An
    expression similar to this might work: ="''#''" Another alternative
    (more reliable, though more work) would be to use a StreamReader and
    StreamWriter after the fact (after exporting the report to a given
    format) to read in the report file into a string or stringbuilder,
    then use String.Replace() (or String.Format()) and then output the
    file with the quote identifiers for certain fields. Hope this helps.
    Regards,
    Enrique Martinez
    Sr. Software Consultant

    Sunday, March 11, 2012

    Controlling export formats based on user role

    I'm using Reporting Services 2000 and I have a situation where I need to specify the file formats available to export. I know I can change the RSReportServer.config file to change these at a global level, but what I really need is to be able to configure these differently for different user groups. For example:

    group 1 - has all available export formats available

    group 2 - only has the ability to export to excel

    group 3 - has no option to export to any format.

    Is there a way for this to be done? If so, can anybody give me any pointers on how it can be done?

    Any help given will be most appreciated!!

    In Report Manager there is no way to limit export formats by role.

    You would need to create a custom application to host the reporting services control, and limit your users in this way.

    cheers,

    Andrew

    |||

    ok thanks I thought this might be the case but just wondered if any simpler solutions were about.

    Thanks again for your reply

    Thursday, March 8, 2012

    Control parameter help......

    I'm writing a page to do some reporting off of one of our databases, and I'm using 3 control parameters for my sql query that feeds my datagrid. 2 of the ControlParameters are from dropdownlists, and one is from a RadioButtonList. The ControlParameters that reference the dropdownlists are both working well, but the one for the RadioButtonList is not.

    The error I am getting is:Exception Details:System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near'@.Booga'. (I changed my ControlParameter name from DateStr to Booga to try to avoid any conflicts with reserved words.)

    Any ideas? Here is a snippet of my code:

    <

    formid="form1"runat="server"><tablewidth="100%"><tr><tdstyle="text-align: center"><strong>DataCenter</strong></td><tdstyle="text-align: center"><strong>Time Scope</strong></td><tdstyle="text-align: center"><strong>Call Status</strong></td></tr><tr><tdstyle="height: 47px; text-align: center"><asp:DropDownListID="DropDownList1"DataSourceID="SqlDataSource2"AutoPostBack="true"DataTextField="LocationName"runat="server"/></td><tdstyle="height: 47px; text-align: center"><asp:RadioButtonListID="RadioButtonList1"runat="server"AutoPostBack="true"Font-Size="Smaller"><asp:ListItemSelected="True"Value=" 1 = 1 ">All</asp:ListItem><asp:ListItemValue="((CAST(CallLog.RecvdDate AS smalldatetime) + 1) >= (CAST(GETDATE() AS smalldatetime)))">Last Day</asp:ListItem><asp:ListItemValue="((CAST(CallLog.RecvdDate AS smalldatetime) + 7) >= (CAST(GETDATE() AS smalldatetime)))">Last Week</asp:ListItem><asp:ListItemValue="((CAST(CallLog.RecvdDate AS smalldatetime) + 30) >= (CAST(GETDATE() AS smalldatetime)))">Last 30 Days</asp:ListItem><asp:ListItemValue="((CAST(CallLog.RecvdDate AS smalldatetime) + 120) >= (CAST(GETDATE() AS smalldatetime)))">Last 120 Days</asp:ListItem></asp:RadioButtonList></td><tdstyle="height: 47px; text-align: center"><asp:DropDownListID="DropDownList2"AutoPostBack="true"runat="server"><asp:ListItemSelected="True"Value="Open">Open</asp:ListItem><asp:ListItemValue="Closed">Closed</asp:ListItem></asp:DropDownList></td></tr></table><br/><pstyle="text-align: center"><strong>Displaying All Open Tickets</strong><br/></p><asp:SqlDataSourceID="SqlDataSource2"runat="server"SelectCommand="SELECT DISTINCT [locationname] FROM [profile]"ConnectionString="<%$ ConnectionStrings:Heat %>"/><tablewidth="100%"><trwidth="100%"><tdvalign="top"width="100%"><asp:GridViewID="GridView1"AllowSorting="True"runat="server"DataSourceID="SqlDataSource1"DataKeyNames="CallID"AutoGenerateColumns="False"Font-Size="Smaller"Width="100%"><Columns><asp:CommandField/><asp:BoundFieldDataField="CallID"HeaderText="Call ID"ReadOnly="True"SortExpression="CallID"/><asp:BoundFieldDataField="CustID"HeaderText="Customer ID"ReadOnly="True"SortExpression="CustID"/><asp:BoundFieldDataField="CallType"HeaderText="Call Type"ReadOnly="True"SortExpression="CallType"/><asp:BoundFieldDataField="Priority"HeaderText="Priority"ReadOnly="True"SortExpression="Priority"/><asp:BoundFieldDataField="Cause"HeaderText="Cause"ReadOnly="True"SortExpression="Cause"/><asp:BoundFieldDataField="CallDesc"HeaderText="Call Description"ReadOnly="True"SortExpression="CallDesc"><ItemStyleWidth=40%/></asp:BoundField><asp:BoundFieldDataField="RecvdBy"HeaderText="Received By"ReadOnly="True"SortExpression="RecvdBy"/><asp:BoundFieldDataField="RecvdDate"HeaderText="Call Date"ReadOnly="True"SortExpression="RecvdDate"><ItemStyleWrap="False"/></asp:BoundField><asp:BoundFieldDataField="RecvdTime"HeaderText="Call Time"ReadOnly="True"SortExpression="RecvdTime"><ItemStyleWrap="False"/></asp:BoundField></Columns></asp:GridView>

    <asp:SqlDataSourceID="SqlDataSource1"runat="server"SelectCommand="SELECT * FROM CallLog INNER JOIN Profile ON CallLog.CustID = Profile.CustID WHERE (Profile.LocationName = @.LocationName) AND (CallLog.CallStatus = @.CallStatus) AND @.Booga "ConnectionString="<%$ ConnectionStrings:Heat %>"><SelectParameters><asp:ControlParameterControlID="DropDownList1"Name="LocationName"PropertyName="SelectedValue"Type="String"/><asp:ControlParameterControlID="DropDownList2"Name="CallStatus"PropertyName="SelectedValue"Type="String"/><asp:ControlParameterControlID="RadioButtonList1"Name="Booga"PropertyName="SelectedValue"Type="String"/></SelectParameters></asp:SqlDataSource></td></tr></table><br/><br/><br/>

    <br />

    </form>

    Your SQL looks incomplete:

    <asp:SqlDataSourceID="SqlDataSource1"runat="server"SelectCommand="SELECT * FROM CallLog INNER JOIN Profile ON CallLog.CustID = Profile.CustID WHERE (Profile.LocationName = @.LocationName) AND (CallLog.CallStatus = @.CallStatus) AND @.Booga "

    What is @.Boonga supposed to be related to?

    |||

    That's where the ControlParameter problem is:

    <asp:ControlParameterControlID="RadioButtonList1"Name="Booga"PropertyName="SelectedValue"Type="String"/>

    That should be making the SQL command end with one of the date comparisons that are controlled by the radiobuttonlist control.

    Wednesday, March 7, 2012

    Continue SP after Database Access Failure

    Over night we take a copy of various live SQL databases onto another SQL
    server for reporting purposes.
    I have a stored procedure that compares the latest live data against the 1
    day old copies to ensure that they are up to date.
    I connect to the live databases using linked servers.
    Here's where the problem is - when one of the external links is down or one
    of the live databases is offline the stored procedure has an error and stops
    .
    How can I test within the stored procedure that the database on the linked
    server is available? Then, based on the result, carry out an action?
    Even a simple select statement against an unavailable database halts the
    whole SP even though I've tried breaking the code down into seperate
    transactions, checking for @.@.ERROR > 0, SET XACT_ABORT OFF, the code still
    fails with "SQL Server does not exist or access denied."
    Any advice greatly appreciated.Hi Paula,
    Error handling in SQL Server 2000 is "somewhat" problematic as you have
    seen.
    For these cases, I use the following trick of nesting the execution scopes:
    USE tempdb
    select * from nonexist
    select 'passed after error', @.@.error
    go
    -- batch was terminated without returning the message
    exec ('select * from nonexist')
    select 'passed after error', @.@.error
    go
    -- inner scope was aborted, outer scope continued
    create proc p3 as
    select * from nonexist
    select 'passed after error', @.@.error
    go
    exec p1
    -- batch was terminated without returning the message
    create proc p2 as
    select * from nonexist
    go
    create proc p3 as
    exec p2
    select 'passed after error', @.@.error
    go
    exec p3
    -- inner procedure was aborted, outer procedure continued
    This should work for most cases although some errors will stop and rollback
    the whole batch including outer scopes.
    I have tested it with inaccessible linked servers and it worked fine for me.
    See the following thread for more details:
    http://groups-beta.google.com/group...f3390d2b34758e2
    HTH
    Ami
    "PaulaPompey" <PaulaPompey@.discussions.microsoft.com> wrote in message
    news:752B8EAA-BC40-4B0D-B413-EFC8F94189A7@.microsoft.com...
    > Over night we take a copy of various live SQL databases onto another SQL
    > server for reporting purposes.
    > I have a stored procedure that compares the latest live data against the 1
    > day old copies to ensure that they are up to date.
    > I connect to the live databases using linked servers.
    > Here's where the problem is - when one of the external links is down or
    one
    > of the live databases is offline the stored procedure has an error and
    stops.
    > How can I test within the stored procedure that the database on the linked
    > server is available? Then, based on the result, carry out an action?
    > Even a simple select statement against an unavailable database halts the
    > whole SP even though I've tried breaking the code down into seperate
    > transactions, checking for @.@.ERROR > 0, SET XACT_ABORT OFF, the code still
    > fails with "SQL Server does not exist or access denied."
    > Any advice greatly appreciated.
    >|||Perhaps the object_id(<object> ) function can help. For example, if
    object_id('mydb..mytable') will return an object id if the database and
    table exists, otherwise it will return NULL.
    "PaulaPompey" <PaulaPompey@.discussions.microsoft.com> wrote in message
    news:752B8EAA-BC40-4B0D-B413-EFC8F94189A7@.microsoft.com...
    > Over night we take a copy of various live SQL databases onto another SQL
    > server for reporting purposes.
    > I have a stored procedure that compares the latest live data against the 1
    > day old copies to ensure that they are up to date.
    > I connect to the live databases using linked servers.
    > Here's where the problem is - when one of the external links is down or
    one
    > of the live databases is offline the stored procedure has an error and
    stops.
    > How can I test within the stored procedure that the database on the linked
    > server is available? Then, based on the result, carry out an action?
    > Even a simple select statement against an unavailable database halts the
    > whole SP even though I've tried breaking the code down into seperate
    > transactions, checking for @.@.ERROR > 0, SET XACT_ABORT OFF, the code still
    > fails with "SQL Server does not exist or access denied."
    > Any advice greatly appreciated.
    >|||Works for tables on the local SQL server, but not on Linked Servers, which i
    s
    where I'm having the problem.
    Thanks for the tip anyway.
    Paula
    "JohnnyAppleseed" wrote:

    > Perhaps the object_id(<object> ) function can help. For example, if
    > object_id('mydb..mytable') will return an object id if the database and
    > table exists, otherwise it will return NULL.
    >
    > "PaulaPompey" <PaulaPompey@.discussions.microsoft.com> wrote in message
    > news:752B8EAA-BC40-4B0D-B413-EFC8F94189A7@.microsoft.com...
    > one
    > stops.
    >
    >|||Paula
    You can check PING Server to make sure that remote server is UP or DOWN
    set nocount on
    CREATE TABLE #t_ip (ip varchar(255))
    DECLARE @.PingSql varchar(1000)
    SELECT @.PingSql = 'ping ' + '00.00.0.0'
    INSERT INTO #t_ip EXEC master.dbo.xp_cmdshell @.PingSql
    SELECT * FROM #t_ip
    IF EXISTS (SELECT TOP 2 * FROM #t_ip WHERE IP = 'Request timed out' )
    BEGIN
    'Do something'
    END
    DROP TABLE #t_ip
    "PaulaPompey" <PaulaPompey@.discussions.microsoft.com> wrote in message
    news:2D613817-450D-45B4-8EE2-D0B0B849D4E5@.microsoft.com...
    > Works for tables on the local SQL server, but not on Linked Servers, which
    is
    > where I'm having the problem.
    > Thanks for the tip anyway.
    > Paula
    > "JohnnyAppleseed" wrote:
    >
    SQL
    the 1
    or
    linked
    the
    still|||Abolutely great! I was over complicating things for my self instead of
    breaking the problem down. I will now be pinging the server using your
    helpful code, then testing for the database using another great persons
    suggestions from this wonderful resource!
    Thanks again
    Paula
    "Uri Dimant" wrote:

    > Paula
    > You can check PING Server to make sure that remote server is UP or DOWN
    > set nocount on
    > CREATE TABLE #t_ip (ip varchar(255))
    > DECLARE @.PingSql varchar(1000)
    > SELECT @.PingSql = 'ping ' + '00.00.0.0'
    > INSERT INTO #t_ip EXEC master.dbo.xp_cmdshell @.PingSql
    > SELECT * FROM #t_ip
    > IF EXISTS (SELECT TOP 2 * FROM #t_ip WHERE IP = 'Request timed out' )
    > BEGIN
    > 'Do something'
    > END
    > DROP TABLE #t_ip
    >
    > "PaulaPompey" <PaulaPompey@.discussions.microsoft.com> wrote in message
    > news:2D613817-450D-45B4-8EE2-D0B0B849D4E5@.microsoft.com...
    > is
    > SQL
    > the 1
    > or
    > linked
    > the
    > still
    >
    >

    Sunday, February 19, 2012

    consuming parameters

    Hi
    I open a report using an url. In the end of the url i add &TEST=123. In
    Reporting Services I have added the @.TEST parameter. I would like this
    parameter to get the value of 123. How do I consume the parameter in the url?
    Do I use the Report Parameters dialog in Reporting Services?
    Please Help
    JuliaJulia:
    I'll take a stab at answering this for you... I hope this will help give you
    a push in the right direction.
    ok, you are sorta on the right track... but what you are going to need to do
    is look into the reporting services documentation and specifically at passing
    values to the report service. For instance, if you go to your report manager
    and click on a report, you will see at the top that there are indeed
    parameters being passed to the report service like your query string example
    that you have. However, there is a specific format that you need to follow,
    that is where the documentation on the reporting service will help.
    See, there essentially 2 parts to the parameters that get passed in... the
    first being the options for how the report will display (like showing
    different options in the toolbar, showing the parameter prompts, ect) and the
    second being values that you are supplying to parameters in your report.
    The documentation on this is a little confusing at first, but stick with
    it... you will be able to get it working.
    I "hope" that this helps.
    "Julia" wrote:
    > Hi
    > I open a report using an url. In the end of the url i add &TEST=123. In
    > Reporting Services I have added the @.TEST parameter. I would like this
    > parameter to get the value of 123. How do I consume the parameter in the url?
    > Do I use the Report Parameters dialog in Reporting Services?
    > Please Help
    > Julia|||One other point. Julia. You are putting the cart before the horse. Before
    making any attempt to call a report via a URL you should first get the
    report working. You need to create a report with query parameters. RS
    automatically creates the report parameters for you when you do this. It is
    important to realize the difference between query and report parameters.
    Until you have a working report don't go anywhere near URL integration.
    Bruce Loehle-Conger
    MVP SQL Server Reporting Services
    "Mark" <Mark@.discussions.microsoft.com> wrote in message
    news:5493C3E7-4070-4018-960B-776EF9EDC4DD@.microsoft.com...
    > Julia:
    > I'll take a stab at answering this for you... I hope this will help give
    > you
    > a push in the right direction.
    > ok, you are sorta on the right track... but what you are going to need to
    > do
    > is look into the reporting services documentation and specifically at
    > passing
    > values to the report service. For instance, if you go to your report
    > manager
    > and click on a report, you will see at the top that there are indeed
    > parameters being passed to the report service like your query string
    > example
    > that you have. However, there is a specific format that you need to
    > follow,
    > that is where the documentation on the reporting service will help.
    > See, there essentially 2 parts to the parameters that get passed in... the
    > first being the options for how the report will display (like showing
    > different options in the toolbar, showing the parameter prompts, ect) and
    > the
    > second being values that you are supplying to parameters in your report.
    > The documentation on this is a little confusing at first, but stick with
    > it... you will be able to get it working.
    > I "hope" that this helps.
    > "Julia" wrote:
    >> Hi
    >> I open a report using an url. In the end of the url i add &TEST=123. In
    >> Reporting Services I have added the @.TEST parameter. I would like this
    >> parameter to get the value of 123. How do I consume the parameter in the
    >> url?
    >> Do I use the Report Parameters dialog in Reporting Services?
    >> Please Help
    >> Julia|||VHi
    And thanks for the answers!
    Ok, I have a working report that I can open from Report Manager. The report
    shows data for one order that I have in my databse. In the databse I have
    many orders so I would like to send the orderId to the select string (select
    a, b, c from Order where OrderId = @.TEST). This is working from Report
    Manager but then the user needs to add the orderId manually and press View
    Report.
    The user will work with an asp.net application and select an order from a
    list and then press a button (or link) to view the order. I need to send the
    orderId to the report. I have tried to add ?TEST=123 in the end of the URL
    but that doesn't work. I would really need an example that I could run in my
    development environment to see how this should work.
    Thanks again
    Julia
    "Bruce L-C [MVP]" wrote:
    > One other point. Julia. You are putting the cart before the horse. Before
    > making any attempt to call a report via a URL you should first get the
    > report working. You need to create a report with query parameters. RS
    > automatically creates the report parameters for you when you do this. It is
    > important to realize the difference between query and report parameters.
    > Until you have a working report don't go anywhere near URL integration.
    >
    > --
    > Bruce Loehle-Conger
    > MVP SQL Server Reporting Services
    >
    > "Mark" <Mark@.discussions.microsoft.com> wrote in message
    > news:5493C3E7-4070-4018-960B-776EF9EDC4DD@.microsoft.com...
    > > Julia:
    > >
    > > I'll take a stab at answering this for you... I hope this will help give
    > > you
    > > a push in the right direction.
    > >
    > > ok, you are sorta on the right track... but what you are going to need to
    > > do
    > > is look into the reporting services documentation and specifically at
    > > passing
    > > values to the report service. For instance, if you go to your report
    > > manager
    > > and click on a report, you will see at the top that there are indeed
    > > parameters being passed to the report service like your query string
    > > example
    > > that you have. However, there is a specific format that you need to
    > > follow,
    > > that is where the documentation on the reporting service will help.
    > >
    > > See, there essentially 2 parts to the parameters that get passed in... the
    > > first being the options for how the report will display (like showing
    > > different options in the toolbar, showing the parameter prompts, ect) and
    > > the
    > > second being values that you are supplying to parameters in your report.
    > >
    > > The documentation on this is a little confusing at first, but stick with
    > > it... you will be able to get it working.
    > >
    > > I "hope" that this helps.
    > >
    > > "Julia" wrote:
    > >
    > >> Hi
    > >>
    > >> I open a report using an url. In the end of the url i add &TEST=123. In
    > >> Reporting Services I have added the @.TEST parameter. I would like this
    > >> parameter to get the value of 123. How do I consume the parameter in the
    > >> url?
    > >> Do I use the Report Parameters dialog in Reporting Services?
    > >>
    > >> Please Help
    > >> Julia
    >
    >

    Tuesday, February 14, 2012

    Constraints and query plans

    Does having enforced foreign key constraints between tables help query
    performance? In our reporting database we have many views with multiple
    joins so that report writing is easier. But with each additional join the
    optimizer generally scans or seeks the index on all joined tables whether or
    not the query requests columns from the table. Currently there are no
    foreign key constraints, by adding and enforcing them would the queries
    produce better plans?
    Thanks,
    Danny
    It depends on the query, but in some cases the optimizer does take advantage
    of constraints. Also, you may want to consider indexing some of your FK
    columns.
    Tom
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    ..
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:iqbUf.3553$4N1.230@.trnddc06...
    Does having enforced foreign key constraints between tables help query
    performance? In our reporting database we have many views with multiple
    joins so that report writing is easier. But with each additional join the
    optimizer generally scans or seeks the index on all joined tables whether or
    not the query requests columns from the table. Currently there are no
    foreign key constraints, by adding and enforcing them would the queries
    produce better plans?
    Thanks,
    Danny
    |||Danny
    > Does having enforced foreign key constraints between tables help query
    > performance?
    Actually NO. However it is a good practice to create an index on FK column
    and then it does improve perfomance.
    FK is a logical concept. It prevents from an unexpectred deletion for
    example.
    Please read an article about FK in the BOL get a whole picture.
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >
    |||Can you give me a basic example of where the optimizer would take advantage
    of a foreign key constraint? I understand creating indexes on the colums.
    In any cases does it decide not to seek or scan an index because of a
    constraint is in place? Or is it that the optimizer has more information
    for find the optimal plan where as with just indexes it may stop and choose
    a plan that is good enough?
    Our views get very complex due to the number of joins. When a query has
    more than about six joins the number of potential plans is really large and
    sometimes the resulting plan is not optimal. We are hoping that in 2005 the
    optimizer does a better job with many joins.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23foIBzaTGHA.792@.TK2MSFTNGP10.phx.gbl...
    > It depends on the query, but in some cases the optimizer does take
    > advantage
    > of constraints. Also, you may want to consider indexing some of your FK
    > columns.
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or
    > not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >
    |||IIRC, doing a WHERE EXISTS/NOT EXISTS can be expedited with a FK in some
    circumstances. Here's an example. Run the following script with Show
    Execution Plan turned on (Ctrl+K):
    use tempdb
    go
    select
    *
    into
    Orders
    from
    Northwind.dbo.Orders
    select
    *
    into
    OrderDetails
    from
    Northwind.dbo.[Order Details]
    alter table Orders
    add
    constraint PK_Orders primary key (OrderID)
    alter table OrderDetails
    add
    constraint PK_OrderDetails primary key (OrderID, ProductID)
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    alter table OrderDetails
    add
    constraint FK1_OrderDetails foreign key (OrderID) references Orders
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    the last two SELECT's are identical, but the second one has a lower query
    cost.
    Also, CHECK constraints do make a difference in partitioned views, since
    only the tables whose CHECK constraints satisfy the search criteria are
    tapped.
    In 2005, there are plan guides that may be of assistance to you:
    http://msdn2.microsoft.com/en-us/library/ms190417(en-US,SQL.90).aspx
    Tom
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    ..
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:lWkUf.8672$I7.2391@.trnddc03...
    Can you give me a basic example of where the optimizer would take advantage
    of a foreign key constraint? I understand creating indexes on the colums.
    In any cases does it decide not to seek or scan an index because of a
    constraint is in place? Or is it that the optimizer has more information
    for find the optimal plan where as with just indexes it may stop and choose
    a plan that is good enough?
    Our views get very complex due to the number of joins. When a query has
    more than about six joins the number of potential plans is really large and
    sometimes the resulting plan is not optimal. We are hoping that in 2005 the
    optimizer does a better job with many joins.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23foIBzaTGHA.792@.TK2MSFTNGP10.phx.gbl...
    > It depends on the query, but in some cases the optimizer does take
    > advantage
    > of constraints. Also, you may want to consider indexing some of your FK
    > columns.
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or
    > not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >
    |||Sorry about that but the example I gave you doesn't produce the desired
    result. (I was comparing the query cost of the FK build with the SELECT.)
    The rest of the commentary still stands. I'll see if I can conjure up some
    code.
    Tom
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    ..
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:O9PnB1gTGHA.6048@.TK2MSFTNGP11.phx.gbl...
    IIRC, doing a WHERE EXISTS/NOT EXISTS can be expedited with a FK in some
    circumstances. Here's an example. Run the following script with Show
    Execution Plan turned on (Ctrl+K):
    use tempdb
    go
    select
    *
    into
    Orders
    from
    Northwind.dbo.Orders
    select
    *
    into
    OrderDetails
    from
    Northwind.dbo.[Order Details]
    alter table Orders
    add
    constraint PK_Orders primary key (OrderID)
    alter table OrderDetails
    add
    constraint PK_OrderDetails primary key (OrderID, ProductID)
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    alter table OrderDetails
    add
    constraint FK1_OrderDetails foreign key (OrderID) references Orders
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    the last two SELECT's are identical, but the second one has a lower query
    cost.
    Also, CHECK constraints do make a difference in partitioned views, since
    only the tables whose CHECK constraints satisfy the search criteria are
    tapped.
    In 2005, there are plan guides that may be of assistance to you:
    http://msdn2.microsoft.com/en-us/library/ms190417(en-US,SQL.90).aspx
    Tom
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    ..
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:lWkUf.8672$I7.2391@.trnddc03...
    Can you give me a basic example of where the optimizer would take advantage
    of a foreign key constraint? I understand creating indexes on the colums.
    In any cases does it decide not to seek or scan an index because of a
    constraint is in place? Or is it that the optimizer has more information
    for find the optimal plan where as with just indexes it may stop and choose
    a plan that is good enough?
    Our views get very complex due to the number of joins. When a query has
    more than about six joins the number of potential plans is really large and
    sometimes the resulting plan is not optimal. We are hoping that in 2005 the
    optimizer does a better job with many joins.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23foIBzaTGHA.792@.TK2MSFTNGP10.phx.gbl...
    > It depends on the query, but in some cases the optimizer does take
    > advantage
    > of constraints. Also, you may want to consider indexing some of your FK
    > columns.
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or
    > not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >
    |||And here it is! :-) Basically, I just changed the NOT EXISTS to EXISTS.
    In the query plan, note that the SELECT after the FK has been added does not
    refer to the Orders table at all:
    select
    *
    into
    Orders
    from
    Northwind.dbo.Orders
    select
    *
    into
    OrderDetails
    from
    Northwind.dbo.[Order Details]
    alter table Orders
    add
    constraint PK_Orders primary key (OrderID)
    alter table OrderDetails
    add
    constraint PK_OrderDetails primary key (OrderID, ProductID)
    go
    select
    *
    from
    OrderDetails od
    where exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    alter table OrderDetails
    add
    constraint FK1_OrderDetails foreign key (OrderID) references Orders
    go
    select
    *
    from
    OrderDetails od
    where exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    drop table OrderDetails, Orders
    Tom
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    ..
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:eXmNc6gTGHA.2656@.TK2MSFTNGP10.phx.gbl...
    Sorry about that but the example I gave you doesn't produce the desired
    result. (I was comparing the query cost of the FK build with the SELECT.)
    The rest of the commentary still stands. I'll see if I can conjure up some
    code.
    Tom
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    ..
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:O9PnB1gTGHA.6048@.TK2MSFTNGP11.phx.gbl...
    IIRC, doing a WHERE EXISTS/NOT EXISTS can be expedited with a FK in some
    circumstances. Here's an example. Run the following script with Show
    Execution Plan turned on (Ctrl+K):
    use tempdb
    go
    select
    *
    into
    Orders
    from
    Northwind.dbo.Orders
    select
    *
    into
    OrderDetails
    from
    Northwind.dbo.[Order Details]
    alter table Orders
    add
    constraint PK_Orders primary key (OrderID)
    alter table OrderDetails
    add
    constraint PK_OrderDetails primary key (OrderID, ProductID)
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    alter table OrderDetails
    add
    constraint FK1_OrderDetails foreign key (OrderID) references Orders
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    the last two SELECT's are identical, but the second one has a lower query
    cost.
    Also, CHECK constraints do make a difference in partitioned views, since
    only the tables whose CHECK constraints satisfy the search criteria are
    tapped.
    In 2005, there are plan guides that may be of assistance to you:
    http://msdn2.microsoft.com/en-us/library/ms190417(en-US,SQL.90).aspx
    Tom
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    ..
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:lWkUf.8672$I7.2391@.trnddc03...
    Can you give me a basic example of where the optimizer would take advantage
    of a foreign key constraint? I understand creating indexes on the colums.
    In any cases does it decide not to seek or scan an index because of a
    constraint is in place? Or is it that the optimizer has more information
    for find the optimal plan where as with just indexes it may stop and choose
    a plan that is good enough?
    Our views get very complex due to the number of joins. When a query has
    more than about six joins the number of potential plans is really large and
    sometimes the resulting plan is not optimal. We are hoping that in 2005 the
    optimizer does a better job with many joins.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23foIBzaTGHA.792@.TK2MSFTNGP10.phx.gbl...
    > It depends on the query, but in some cases the optimizer does take
    > advantage
    > of constraints. Also, you may want to consider indexing some of your FK
    > columns.
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or
    > not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >
    |||Actually, YES:
    http://www.microsoft.com/technet/abo...ps_122104.mspx
    Tom
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    ..
    "Uri Dimant" <urid@.iscar.co.il> wrote in message
    news:%23fbPQzaTGHA.4140@.TK2MSFTNGP10.phx.gbl...
    Danny
    > Does having enforced foreign key constraints between tables help query
    > performance?
    Actually NO. However it is a good practice to create an index on FK column
    and then it does improve perfomance.
    FK is a logical concept. It prevents from an unexpectred deletion for
    example.
    Please read an article about FK in the BOL get a whole picture.
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >
    |||In a large reporting environment, the differences between foreign key
    constraints when using views is usually negligible.
    In other words, the solution I think you are using is a bunch of large
    canned views showing a gazillion columns, and then reports pick and
    choose teh data and columns they really need from that view.
    These views are VERY slow as the optimizer has a tough time figuring
    out which of the gazillion indexes to utilize to get the right data.
    The next step is to pass parameters to a stored procedure for
    frequently used, particularly slow queries. By simply moving the code
    from a view to a stored procedure, speed will come back.
    In the longer run, if you can afford it, OLAP is the BEST reporting
    solution for analysts. It is sooooo much faster, it is unreal, but
    there is a learning curve for all involved.
    |||Thanks.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23iS339sTGHA.4900@.TK2MSFTNGP12.phx.gbl...
    > Actually, YES:
    > http://www.microsoft.com/technet/abo...ps_122104.mspx
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Uri Dimant" <urid@.iscar.co.il> wrote in message
    > news:%23fbPQzaTGHA.4140@.TK2MSFTNGP10.phx.gbl...
    > Danny
    > Actually NO. However it is a good practice to create an index on FK column
    > and then it does improve perfomance.
    > FK is a logical concept. It prevents from an unexpectred deletion for
    > example.
    > Please read an article about FK in the BOL get a whole picture.
    >
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    >

    Constraints and query plans

    Does having enforced foreign key constraints between tables help query
    performance? In our reporting database we have many views with multiple
    joins so that report writing is easier. But with each additional join the
    optimizer generally scans or seeks the index on all joined tables whether or
    not the query requests columns from the table. Currently there are no
    foreign key constraints, by adding and enforcing them would the queries
    produce better plans?
    Thanks,
    DannyIt depends on the query, but in some cases the optimizer does take advantage
    of constraints. Also, you may want to consider indexing some of your FK
    columns.
    --
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:iqbUf.3553$4N1.230@.trnddc06...
    Does having enforced foreign key constraints between tables help query
    performance? In our reporting database we have many views with multiple
    joins so that report writing is easier. But with each additional join the
    optimizer generally scans or seeks the index on all joined tables whether or
    not the query requests columns from the table. Currently there are no
    foreign key constraints, by adding and enforcing them would the queries
    produce better plans?
    Thanks,
    Danny|||Danny
    > Does having enforced foreign key constraints between tables help query
    > performance?
    Actually NO. However it is a good practice to create an index on FK column
    and then it does improve perfomance.
    FK is a logical concept. It prevents from an unexpectred deletion for
    example.
    Please read an article about FK in the BOL get a whole picture.
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >|||Can you give me a basic example of where the optimizer would take advantage
    of a foreign key constraint? I understand creating indexes on the colums.
    In any cases does it decide not to seek or scan an index because of a
    constraint is in place? Or is it that the optimizer has more information
    for find the optimal plan where as with just indexes it may stop and choose
    a plan that is good enough?
    Our views get very complex due to the number of joins. When a query has
    more than about six joins the number of potential plans is really large and
    sometimes the resulting plan is not optimal. We are hoping that in 2005 the
    optimizer does a better job with many joins.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23foIBzaTGHA.792@.TK2MSFTNGP10.phx.gbl...
    > It depends on the query, but in some cases the optimizer does take
    > advantage
    > of constraints. Also, you may want to consider indexing some of your FK
    > columns.
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or
    > not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >|||IIRC, doing a WHERE EXISTS/NOT EXISTS can be expedited with a FK in some
    circumstances. Here's an example. Run the following script with Show
    Execution Plan turned on (Ctrl+K):
    use tempdb
    go
    select
    *
    into
    Orders
    from
    Northwind.dbo.Orders
    select
    *
    into
    OrderDetails
    from
    Northwind.dbo.[Order Details]
    alter table Orders
    add
    constraint PK_Orders primary key (OrderID)
    alter table OrderDetails
    add
    constraint PK_OrderDetails primary key (OrderID, ProductID)
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    alter table OrderDetails
    add
    constraint FK1_OrderDetails foreign key (OrderID) references Orders
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    the last two SELECT's are identical, but the second one has a lower query
    cost.
    Also, CHECK constraints do make a difference in partitioned views, since
    only the tables whose CHECK constraints satisfy the search criteria are
    tapped.
    In 2005, there are plan guides that may be of assistance to you:
    http://msdn2.microsoft.com/en-us/library/ms190417(en-US,SQL.90).aspx
    --
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:lWkUf.8672$I7.2391@.trnddc03...
    Can you give me a basic example of where the optimizer would take advantage
    of a foreign key constraint? I understand creating indexes on the colums.
    In any cases does it decide not to seek or scan an index because of a
    constraint is in place? Or is it that the optimizer has more information
    for find the optimal plan where as with just indexes it may stop and choose
    a plan that is good enough?
    Our views get very complex due to the number of joins. When a query has
    more than about six joins the number of potential plans is really large and
    sometimes the resulting plan is not optimal. We are hoping that in 2005 the
    optimizer does a better job with many joins.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23foIBzaTGHA.792@.TK2MSFTNGP10.phx.gbl...
    > It depends on the query, but in some cases the optimizer does take
    > advantage
    > of constraints. Also, you may want to consider indexing some of your FK
    > columns.
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or
    > not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >|||Sorry about that but the example I gave you doesn't produce the desired
    result. (I was comparing the query cost of the FK build with the SELECT.)
    The rest of the commentary still stands. I'll see if I can conjure up some
    code.
    --
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:O9PnB1gTGHA.6048@.TK2MSFTNGP11.phx.gbl...
    IIRC, doing a WHERE EXISTS/NOT EXISTS can be expedited with a FK in some
    circumstances. Here's an example. Run the following script with Show
    Execution Plan turned on (Ctrl+K):
    use tempdb
    go
    select
    *
    into
    Orders
    from
    Northwind.dbo.Orders
    select
    *
    into
    OrderDetails
    from
    Northwind.dbo.[Order Details]
    alter table Orders
    add
    constraint PK_Orders primary key (OrderID)
    alter table OrderDetails
    add
    constraint PK_OrderDetails primary key (OrderID, ProductID)
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    alter table OrderDetails
    add
    constraint FK1_OrderDetails foreign key (OrderID) references Orders
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    the last two SELECT's are identical, but the second one has a lower query
    cost.
    Also, CHECK constraints do make a difference in partitioned views, since
    only the tables whose CHECK constraints satisfy the search criteria are
    tapped.
    In 2005, there are plan guides that may be of assistance to you:
    http://msdn2.microsoft.com/en-us/library/ms190417(en-US,SQL.90).aspx
    --
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:lWkUf.8672$I7.2391@.trnddc03...
    Can you give me a basic example of where the optimizer would take advantage
    of a foreign key constraint? I understand creating indexes on the colums.
    In any cases does it decide not to seek or scan an index because of a
    constraint is in place? Or is it that the optimizer has more information
    for find the optimal plan where as with just indexes it may stop and choose
    a plan that is good enough?
    Our views get very complex due to the number of joins. When a query has
    more than about six joins the number of potential plans is really large and
    sometimes the resulting plan is not optimal. We are hoping that in 2005 the
    optimizer does a better job with many joins.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23foIBzaTGHA.792@.TK2MSFTNGP10.phx.gbl...
    > It depends on the query, but in some cases the optimizer does take
    > advantage
    > of constraints. Also, you may want to consider indexing some of your FK
    > columns.
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or
    > not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >|||And here it is! :-) Basically, I just changed the NOT EXISTS to EXISTS.
    In the query plan, note that the SELECT after the FK has been added does not
    refer to the Orders table at all:
    select
    *
    into
    Orders
    from
    Northwind.dbo.Orders
    select
    *
    into
    OrderDetails
    from
    Northwind.dbo.[Order Details]
    alter table Orders
    add
    constraint PK_Orders primary key (OrderID)
    alter table OrderDetails
    add
    constraint PK_OrderDetails primary key (OrderID, ProductID)
    go
    select
    *
    from
    OrderDetails od
    where exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    alter table OrderDetails
    add
    constraint FK1_OrderDetails foreign key (OrderID) references Orders
    go
    select
    *
    from
    OrderDetails od
    where exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    drop table OrderDetails, Orders
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:eXmNc6gTGHA.2656@.TK2MSFTNGP10.phx.gbl...
    Sorry about that but the example I gave you doesn't produce the desired
    result. (I was comparing the query cost of the FK build with the SELECT.)
    The rest of the commentary still stands. I'll see if I can conjure up some
    code.
    --
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:O9PnB1gTGHA.6048@.TK2MSFTNGP11.phx.gbl...
    IIRC, doing a WHERE EXISTS/NOT EXISTS can be expedited with a FK in some
    circumstances. Here's an example. Run the following script with Show
    Execution Plan turned on (Ctrl+K):
    use tempdb
    go
    select
    *
    into
    Orders
    from
    Northwind.dbo.Orders
    select
    *
    into
    OrderDetails
    from
    Northwind.dbo.[Order Details]
    alter table Orders
    add
    constraint PK_Orders primary key (OrderID)
    alter table OrderDetails
    add
    constraint PK_OrderDetails primary key (OrderID, ProductID)
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    alter table OrderDetails
    add
    constraint FK1_OrderDetails foreign key (OrderID) references Orders
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    the last two SELECT's are identical, but the second one has a lower query
    cost.
    Also, CHECK constraints do make a difference in partitioned views, since
    only the tables whose CHECK constraints satisfy the search criteria are
    tapped.
    In 2005, there are plan guides that may be of assistance to you:
    http://msdn2.microsoft.com/en-us/library/ms190417(en-US,SQL.90).aspx
    --
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:lWkUf.8672$I7.2391@.trnddc03...
    Can you give me a basic example of where the optimizer would take advantage
    of a foreign key constraint? I understand creating indexes on the colums.
    In any cases does it decide not to seek or scan an index because of a
    constraint is in place? Or is it that the optimizer has more information
    for find the optimal plan where as with just indexes it may stop and choose
    a plan that is good enough?
    Our views get very complex due to the number of joins. When a query has
    more than about six joins the number of potential plans is really large and
    sometimes the resulting plan is not optimal. We are hoping that in 2005 the
    optimizer does a better job with many joins.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23foIBzaTGHA.792@.TK2MSFTNGP10.phx.gbl...
    > It depends on the query, but in some cases the optimizer does take
    > advantage
    > of constraints. Also, you may want to consider indexing some of your FK
    > columns.
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or
    > not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >|||Actually, YES:
    http://www.microsoft.com/technet/abouttn/subscriptions/flash/tips/tips_122104.mspx
    --
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Uri Dimant" <urid@.iscar.co.il> wrote in message
    news:%23fbPQzaTGHA.4140@.TK2MSFTNGP10.phx.gbl...
    Danny
    > Does having enforced foreign key constraints between tables help query
    > performance?
    Actually NO. However it is a good practice to create an index on FK column
    and then it does improve perfomance.
    FK is a logical concept. It prevents from an unexpectred deletion for
    example.
    Please read an article about FK in the BOL get a whole picture.
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >|||In a large reporting environment, the differences between foreign key
    constraints when using views is usually negligible.
    In other words, the solution I think you are using is a bunch of large
    canned views showing a gazillion columns, and then reports pick and
    choose teh data and columns they really need from that view.
    These views are VERY slow as the optimizer has a tough time figuring
    out which of the gazillion indexes to utilize to get the right data.
    The next step is to pass parameters to a stored procedure for
    frequently used, particularly slow queries. By simply moving the code
    from a view to a stored procedure, speed will come back.
    In the longer run, if you can afford it, OLAP is the BEST reporting
    solution for analysts. It is sooooo much faster, it is unreal, but
    there is a learning curve for all involved.|||Thanks.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23iS339sTGHA.4900@.TK2MSFTNGP12.phx.gbl...
    > Actually, YES:
    > http://www.microsoft.com/technet/abouttn/subscriptions/flash/tips/tips_122104.mspx
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Uri Dimant" <urid@.iscar.co.il> wrote in message
    > news:%23fbPQzaTGHA.4140@.TK2MSFTNGP10.phx.gbl...
    > Danny
    >> Does having enforced foreign key constraints between tables help query
    >> performance?
    > Actually NO. However it is a good practice to create an index on FK column
    > and then it does improve perfomance.
    > FK is a logical concept. It prevents from an unexpectred deletion for
    > example.
    > Please read an article about FK in the BOL get a whole picture.
    >
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    >> Does having enforced foreign key constraints between tables help query
    >> performance? In our reporting database we have many views with multiple
    >> joins so that report writing is easier. But with each additional join
    >> the
    >> optimizer generally scans or seeks the index on all joined tables whether
    >> or not the query requests columns from the table. Currently there are no
    >> foreign key constraints, by adding and enforcing them would the queries
    >> produce better plans?
    >> Thanks,
    >> Danny
    >

    Constraints and query plans

    Does having enforced foreign key constraints between tables help query
    performance? In our reporting database we have many views with multiple
    joins so that report writing is easier. But with each additional join the
    optimizer generally scans or seeks the index on all joined tables whether or
    not the query requests columns from the table. Currently there are no
    foreign key constraints, by adding and enforcing them would the queries
    produce better plans?
    Thanks,
    DannyIt depends on the query, but in some cases the optimizer does take advantage
    of constraints. Also, you may want to consider indexing some of your FK
    columns.
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:iqbUf.3553$4N1.230@.trnddc06...
    Does having enforced foreign key constraints between tables help query
    performance? In our reporting database we have many views with multiple
    joins so that report writing is easier. But with each additional join the
    optimizer generally scans or seeks the index on all joined tables whether or
    not the query requests columns from the table. Currently there are no
    foreign key constraints, by adding and enforcing them would the queries
    produce better plans?
    Thanks,
    Danny|||Danny
    > Does having enforced foreign key constraints between tables help query
    > performance?
    Actually NO. However it is a good practice to create an index on FK column
    and then it does improve perfomance.
    FK is a logical concept. It prevents from an unexpectred deletion for
    example.
    Please read an article about FK in the BOL get a whole picture.
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >|||Can you give me a basic example of where the optimizer would take advantage
    of a foreign key constraint? I understand creating indexes on the colums.
    In any cases does it decide not to seek or scan an index because of a
    constraint is in place? Or is it that the optimizer has more information
    for find the optimal plan where as with just indexes it may stop and choose
    a plan that is good enough?
    Our views get very complex due to the number of joins. When a query has
    more than about six joins the number of potential plans is really large and
    sometimes the resulting plan is not optimal. We are hoping that in 2005 the
    optimizer does a better job with many joins.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23foIBzaTGHA.792@.TK2MSFTNGP10.phx.gbl...
    > It depends on the query, but in some cases the optimizer does take
    > advantage
    > of constraints. Also, you may want to consider indexing some of your FK
    > columns.
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or
    > not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >|||IIRC, doing a WHERE EXISTS/NOT EXISTS can be expedited with a FK in some
    circumstances. Here's an example. Run the following script with Show
    Execution Plan turned on (Ctrl+K):
    use tempdb
    go
    select
    *
    into
    Orders
    from
    Northwind.dbo.Orders
    select
    *
    into
    OrderDetails
    from
    Northwind.dbo.[Order Details]
    alter table Orders
    add
    constraint PK_Orders primary key (OrderID)
    alter table OrderDetails
    add
    constraint PK_OrderDetails primary key (OrderID, ProductID)
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    alter table OrderDetails
    add
    constraint FK1_OrderDetails foreign key (OrderID) references Orders
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    the last two SELECT's are identical, but the second one has a lower query
    cost.
    Also, CHECK constraints do make a difference in partitioned views, since
    only the tables whose CHECK constraints satisfy the search criteria are
    tapped.
    In 2005, there are plan guides that may be of assistance to you:
    http://msdn2.microsoft.com/en-us/library/ms190417(en-US,SQL.90).aspx
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:lWkUf.8672$I7.2391@.trnddc03...
    Can you give me a basic example of where the optimizer would take advantage
    of a foreign key constraint? I understand creating indexes on the colums.
    In any cases does it decide not to seek or scan an index because of a
    constraint is in place? Or is it that the optimizer has more information
    for find the optimal plan where as with just indexes it may stop and choose
    a plan that is good enough?
    Our views get very complex due to the number of joins. When a query has
    more than about six joins the number of potential plans is really large and
    sometimes the resulting plan is not optimal. We are hoping that in 2005 the
    optimizer does a better job with many joins.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23foIBzaTGHA.792@.TK2MSFTNGP10.phx.gbl...
    > It depends on the query, but in some cases the optimizer does take
    > advantage
    > of constraints. Also, you may want to consider indexing some of your FK
    > columns.
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or
    > not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >|||Sorry about that but the example I gave you doesn't produce the desired
    result. (I was comparing the query cost of the FK build with the SELECT.)
    The rest of the commentary still stands. I'll see if I can conjure up some
    code.
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:O9PnB1gTGHA.6048@.TK2MSFTNGP11.phx.gbl...
    IIRC, doing a WHERE EXISTS/NOT EXISTS can be expedited with a FK in some
    circumstances. Here's an example. Run the following script with Show
    Execution Plan turned on (Ctrl+K):
    use tempdb
    go
    select
    *
    into
    Orders
    from
    Northwind.dbo.Orders
    select
    *
    into
    OrderDetails
    from
    Northwind.dbo.[Order Details]
    alter table Orders
    add
    constraint PK_Orders primary key (OrderID)
    alter table OrderDetails
    add
    constraint PK_OrderDetails primary key (OrderID, ProductID)
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    alter table OrderDetails
    add
    constraint FK1_OrderDetails foreign key (OrderID) references Orders
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    the last two SELECT's are identical, but the second one has a lower query
    cost.
    Also, CHECK constraints do make a difference in partitioned views, since
    only the tables whose CHECK constraints satisfy the search criteria are
    tapped.
    In 2005, there are plan guides that may be of assistance to you:
    http://msdn2.microsoft.com/en-us/library/ms190417(en-US,SQL.90).aspx
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:lWkUf.8672$I7.2391@.trnddc03...
    Can you give me a basic example of where the optimizer would take advantage
    of a foreign key constraint? I understand creating indexes on the colums.
    In any cases does it decide not to seek or scan an index because of a
    constraint is in place? Or is it that the optimizer has more information
    for find the optimal plan where as with just indexes it may stop and choose
    a plan that is good enough?
    Our views get very complex due to the number of joins. When a query has
    more than about six joins the number of potential plans is really large and
    sometimes the resulting plan is not optimal. We are hoping that in 2005 the
    optimizer does a better job with many joins.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23foIBzaTGHA.792@.TK2MSFTNGP10.phx.gbl...
    > It depends on the query, but in some cases the optimizer does take
    > advantage
    > of constraints. Also, you may want to consider indexing some of your FK
    > columns.
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or
    > not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >|||And here it is! :-) Basically, I just changed the NOT EXISTS to EXISTS.
    In the query plan, note that the SELECT after the FK has been added does not
    refer to the Orders table at all:
    select
    *
    into
    Orders
    from
    Northwind.dbo.Orders
    select
    *
    into
    OrderDetails
    from
    Northwind.dbo.[Order Details]
    alter table Orders
    add
    constraint PK_Orders primary key (OrderID)
    alter table OrderDetails
    add
    constraint PK_OrderDetails primary key (OrderID, ProductID)
    go
    select
    *
    from
    OrderDetails od
    where exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    alter table OrderDetails
    add
    constraint FK1_OrderDetails foreign key (OrderID) references Orders
    go
    select
    *
    from
    OrderDetails od
    where exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    drop table OrderDetails, Orders
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:eXmNc6gTGHA.2656@.TK2MSFTNGP10.phx.gbl...
    Sorry about that but the example I gave you doesn't produce the desired
    result. (I was comparing the query cost of the FK build with the SELECT.)
    The rest of the commentary still stands. I'll see if I can conjure up some
    code.
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:O9PnB1gTGHA.6048@.TK2MSFTNGP11.phx.gbl...
    IIRC, doing a WHERE EXISTS/NOT EXISTS can be expedited with a FK in some
    circumstances. Here's an example. Run the following script with Show
    Execution Plan turned on (Ctrl+K):
    use tempdb
    go
    select
    *
    into
    Orders
    from
    Northwind.dbo.Orders
    select
    *
    into
    OrderDetails
    from
    Northwind.dbo.[Order Details]
    alter table Orders
    add
    constraint PK_Orders primary key (OrderID)
    alter table OrderDetails
    add
    constraint PK_OrderDetails primary key (OrderID, ProductID)
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    alter table OrderDetails
    add
    constraint FK1_OrderDetails foreign key (OrderID) references Orders
    go
    select
    *
    from
    OrderDetails od
    where not exists
    (
    select
    *
    from
    Orders o
    where
    o.OrderID = od.OrderID
    )
    go
    the last two SELECT's are identical, but the second one has a lower query
    cost.
    Also, CHECK constraints do make a difference in partitioned views, since
    only the tables whose CHECK constraints satisfy the search criteria are
    tapped.
    In 2005, there are plan guides that may be of assistance to you:
    http://msdn2.microsoft.com/en-us/library/ms190417(en-US,SQL.90).aspx
    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:lWkUf.8672$I7.2391@.trnddc03...
    Can you give me a basic example of where the optimizer would take advantage
    of a foreign key constraint? I understand creating indexes on the colums.
    In any cases does it decide not to seek or scan an index because of a
    constraint is in place? Or is it that the optimizer has more information
    for find the optimal plan where as with just indexes it may stop and choose
    a plan that is good enough?
    Our views get very complex due to the number of joins. When a query has
    more than about six joins the number of potential plans is really large and
    sometimes the resulting plan is not optimal. We are hoping that in 2005 the
    optimizer does a better job with many joins.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23foIBzaTGHA.792@.TK2MSFTNGP10.phx.gbl...
    > It depends on the query, but in some cases the optimizer does take
    > advantage
    > of constraints. Also, you may want to consider indexing some of your FK
    > columns.
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or
    > not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >|||Actually, YES:
    http://www.microsoft.com/technet/ab...04.mspx

    Tom
    ----
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinpub.com
    .
    "Uri Dimant" <urid@.iscar.co.il> wrote in message
    news:%23fbPQzaTGHA.4140@.TK2MSFTNGP10.phx.gbl...
    Danny
    > Does having enforced foreign key constraints between tables help query
    > performance?
    Actually NO. However it is a good practice to create an index on FK column
    and then it does improve perfomance.
    FK is a logical concept. It prevents from an unexpectred deletion for
    example.
    Please read an article about FK in the BOL get a whole picture.
    "Danny" <djscroggins@.verizon.net> wrote in message
    news:iqbUf.3553$4N1.230@.trnddc06...
    > Does having enforced foreign key constraints between tables help query
    > performance? In our reporting database we have many views with multiple
    > joins so that report writing is easier. But with each additional join the
    > optimizer generally scans or seeks the index on all joined tables whether
    > or not the query requests columns from the table. Currently there are no
    > foreign key constraints, by adding and enforcing them would the queries
    > produce better plans?
    > Thanks,
    > Danny
    >|||In a large reporting environment, the differences between foreign key
    constraints when using views is usually negligible.
    In other words, the solution I think you are using is a bunch of large
    canned views showing a gazillion columns, and then reports pick and
    choose teh data and columns they really need from that view.
    These views are VERY slow as the optimizer has a tough time figuring
    out which of the gazillion indexes to utilize to get the right data.
    The next step is to pass parameters to a stored procedure for
    frequently used, particularly slow queries. By simply moving the code
    from a view to a stored procedure, speed will come back.
    In the longer run, if you can afford it, OLAP is the BEST reporting
    solution for analysts. It is sooooo much faster, it is unreal, but
    there is a learning curve for all involved.|||Thanks.
    "Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message
    news:%23iS339sTGHA.4900@.TK2MSFTNGP12.phx.gbl...
    > Actually, YES:
    > http://www.microsoft.com/technet/ab...04.ms
    px
    > --
    > Tom
    > ----
    > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    > SQL Server MVP
    > Columnist, SQL Server Professional
    > Toronto, ON Canada
    > www.pinpub.com
    > .
    > "Uri Dimant" <urid@.iscar.co.il> wrote in message
    > news:%23fbPQzaTGHA.4140@.TK2MSFTNGP10.phx.gbl...
    > Danny
    > Actually NO. However it is a good practice to create an index on FK column
    > and then it does improve perfomance.
    > FK is a logical concept. It prevents from an unexpectred deletion for
    > example.
    > Please read an article about FK in the BOL get a whole picture.
    >
    > "Danny" <djscroggins@.verizon.net> wrote in message
    > news:iqbUf.3553$4N1.230@.trnddc06...
    >