小编Dav*_*idN的帖子

避免WPF中的重复(DRY之后)

考虑以下2个XAML代码段(在文件中并排):

<Button
    x:Name="BuyButton"
    Margin="0,0,1,1"
    IsEnabled="{Binding CanBuy}"
    >
    <StackPanel
        DataContext="{Binding Product}">
        <TextBlock
            Foreground="Red"
            Text="BUY" />
        <TextBlock
            Foreground="Red"
            Text="{Binding BuyPrice}" />
    </StackPanel>
    </Button>

<Button
    x:Name="SellButton"
    Margin="0,0,1,1"
    IsEnabled="{Binding CanSell}"
    >
    <StackPanel
        DataContext="{Binding Product}">
        <TextBlock
            Foreground="Red"
            Text="SELL" />
        <TextBlock
            Foreground="Red"
            Text="{Binding SellPrice}" />
    </StackPanel>
</Button>
Run Code Online (Sandbox Code Playgroud)

如何消除WPF中的重复项?我有大约4种用法(这里显示2种),它们的相似度是80%,甚至更多。我可以将其提取到用户控件中,并在其中放置一些DP,然后我将拥有一个控件,但是我担心我会开始用大量的用户控件来填充我的代码库(我有很多“一次性的” “这样的情况)。我不喜欢这里的DataTemplate解决方案,因为我仍然需要两个模板,这些模板将有重复的代码。如果不创建一堆模板/控件,是否有办法使此代码遵循DRY?

wpf dry

5
推荐指数
1
解决办法
748
查看次数

2个表上的简单内部连接导致错误的估计行和慢性能

我无法理解为什么一个看似简单的查询在2个表上进行内部连接会导致估计的行数为1,而实际值几乎为200万.我没有看到丢失索引的任何问题,并且98%的查询成本发生在索引搜索中.查询运行时,我看不到任何I/O或CPU红色标记,大约需要12秒.添加统计信息似乎没有意义,因为查询只是加入id,并且两个表都将这些ID上的聚簇索引作为主键.

此查询是遇到相同问题的较大查询的简化版本,但我将其归结为此简化查询遇到同样的问题.我假设估计行和实际行之间的差异至少有助于次优查询性能.有趣的是,将LEFT OUTER JOIN替换为INNER JOIN可以获得大致相同的性能,即使它确实将Estimated Rows设置为正确.

查询:

SELECT StationId, Readings.Power, Readings.TimeCovered
FROM  Readings
INNER JOIN Stations ON Readings.StationId = Stations.Id
Run Code Online (Sandbox Code Playgroud)

实际计划XML:

<?xml version="1.0" encoding="utf-16"?>
<ShowPlanXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.1" Build="10.50.1600.1" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
  <BatchSequence>
    <Batch>
      <Statements>
        <StmtSimple StatementCompId="1" StatementEstRows="1" StatementId="1" StatementOptmLevel="FULL" StatementOptmEarlyAbortReason="GoodEnoughPlanFound" StatementSubTreeCost="0.388499" StatementText="SELECT StationId,&#xD;&#xA;[Readings].[Power], [Readings].[TimeCovered]&#xD;&#xA;FROM  [dbo].[Readings]&#xD;&#xA;INNER JOIN [dbo].[Stations] ON [Readings].[StationId] = [Stations].[Id]" StatementType="SELECT" QueryHash="0x540DF2384788314E" QueryPlanHash="0x7546B31B38AC8153">
          <StatementSetOptions ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" ARITHABORT="false" CONCAT_NULL_YIELDS_NULL="true" NUMERIC_ROUNDABORT="false" QUOTED_IDENTIFIER="true" />
          <QueryPlan DegreeOfParallelism="1" CachedPlanSize="24" CompileTime="3" CompileCPU="3" CompileMemory="224">
            <RelOp AvgRowSize="17" EstimateCPU="0.00118294" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="1" LogicalOp="Inner Join" NodeId="0" Parallel="false" PhysicalOp="Nested …
Run Code Online (Sandbox Code Playgroud)

sql sql-server performance join sql-server-2008

5
推荐指数
1
解决办法
682
查看次数

标签 统计

dry ×1

join ×1

performance ×1

sql ×1

sql-server ×1

sql-server-2008 ×1

wpf ×1