Monday 16 October 2017

Moving Average Sql Abfrage


22 SQL für Analyse und Reporting Oracle verfügt über erweiterte analytische Verarbeitungsfunktionen für SQLs, indem eine neue Familie von analytischen SQL-Funktionen eingeführt wird. Mit diesen analytischen Funktionen können Sie berechnen: Ranglisten und Perzentile Verschieben von Fensterberechnungen Lineare Regressionsstatistik Die Rangierfunktionen umfassen kumulative Verteilungen, Prozentrang und N-Kacheln. Bewegliche Fensterberechnungen ermöglichen es Ihnen, verschiebende und kumulative Aggregationen wie Summen und Mittelwerte zu finden. Die Laglead-Analyse ermöglicht direkte Reihenreferenz-Referenzen, so dass Sie Periodenänderungen berechnen können. Mit der Firstlast-Analyse können Sie den ersten oder letzten Wert einer geordneten Gruppe finden. Weitere Erweiterungen für SQL sind der CASE-Ausdruck und der partitionierte äußere Join. CASE-Ausdrücke bieten, wenn-dann Logik nützlich in vielen Situationen. Partitionierte äußere Verknüpfung ist eine Erweiterung der ANSI-Outer-Join-Syntax, die es Benutzern ermöglicht, bestimmte Dimensionen selektiv zu verdichten und andere spärlich zu halten. Auf diese Weise können Reporting-Tools selektiv die Dimensionen verdichten, zum Beispiel diejenigen, die in ihren tabellarischen Berichten erscheinen, während andere spärlich beibehalten werden. Um die Performance zu erhöhen, können analytische Funktionen parallelisiert werden: Mehrere Prozesse können gleichzeitig alle diese Anweisungen ausführen. Diese Fähigkeiten machen Berechnungen einfacher und effizienter, wodurch die Leistung, Skalierbarkeit und Einfachheit der Datenbank verbessert wird. Analytische Funktionen werden wie in Tabelle 22-1 beschrieben klassifiziert. Tabelle 22-1 Analytische Funktionen und ihre Verwendungen Um diese Operationen auszuführen, fügen die analytischen Funktionen der SQL-Verarbeitung mehrere neue Elemente hinzu. Diese Elemente bauen auf vorhandenen SQL auf, um flexible und leistungsfähige Berechnungsausdrücke zu ermöglichen. Mit wenigen Ausnahmen haben die analytischen Funktionen diese neuen Elemente. Der Verarbeitungsablauf ist in Abbildung 22-1 dargestellt. Abbildung 22-1 Verarbeitungsauftrag Die wesentlichen Konzepte, die in analytischen Funktionen verwendet werden, sind: Die Abfrageverarbeitung mit Hilfe von analytischen Funktionen erfolgt in drei Stufen. Erstens, alle Joins, WHERE. GROUP BY und HAVING-Klauseln durchgeführt. Zweitens wird die Ergebnismenge den analytischen Funktionen zur Verfügung gestellt, und alle ihre Berechnungen erfolgen. Drittens, wenn die Abfrage eine ORDER BY-Klausel an ihrem Ende hat, wird die ORDER BY verarbeitet, um eine präzise Ausgabeordnung zu ermöglichen. Die Verarbeitungsreihenfolge ist in Abbildung 22-1 dargestellt. Ergebnismengenpartitionen Die analytischen Funktionen ermöglichen es Benutzern, Abfrageergebnismengen in Gruppen von Zeilen, die als Partitionen bezeichnet werden, zu teilen. Beachten Sie, dass der Begriff Partitionen, die mit analytischen Funktionen verwendet werden, nicht mit dem Tabellenpartitionsmerkmal verknüpft ist. In diesem Kapitel bezieht sich der Begriff Partitionen nur auf die Bedeutung der analytischen Funktionen. Partitionen werden nach den Gruppen definiert, die mit GROUP BY-Klauseln definiert sind, sodass sie für aggregierte Ergebnisse wie Summen und Mittelwerte zur Verfügung stehen. Partitionsbereiche können auf beliebigen Spalten oder Ausdrücken basieren. Eine Abfrageergebnismenge kann in nur eine Partition partitioniert werden, die alle Zeilen, einige große Partitionen oder viele kleine Partitionen mit nur wenigen Zeilen enthält. Für jede Zeile in einer Partition können Sie ein Schiebefenster von Daten definieren. Dieses Fenster bestimmt den Bereich der Zeilen, die für die Berechnung der aktuellen Zeile verwendet werden. Fenstergrößen können entweder auf eine physikalische Anzahl von Zeilen oder ein logisches Intervall wie Zeit basieren. Das Fenster hat eine Anfangszeile und eine Endzeile. Je nach ihrer Definition kann sich das Fenster an einem oder beiden Enden bewegen. Beispielsweise würde ein Fenster, das für eine kumulative Summenfunktion definiert ist, seine Startzeile in der ersten Zeile seiner Partition festgelegt, und seine Endzeile würde vom Anfangspunkt bis zur letzten Zeile der Partition verschoben. Im Gegensatz dazu würde ein Fenster, das für einen gleitenden Durchschnitt definiert ist, sowohl seine Anfangs - als auch seine Endpunkte gleiten lassen, so daß sie einen konstanten physikalischen oder logischen Bereich beibehalten. Ein Fenster kann so groß sein wie alle Zeilen in einer Partition oder nur ein Schiebefenster einer Zeile innerhalb einer Partition. Wenn ein Fenster in der Nähe eines Rahmens ist, gibt die Funktion Ergebnisse nur für die verfügbaren Zeilen zurück, anstatt Sie zu warnen, dass die Ergebnisse nicht das sind, was Sie wollen. Bei der Verwendung von Fensterfunktionen ist die aktuelle Zeile während der Berechnungen enthalten, so dass Sie nur n (n) angeben sollten, wenn Sie mit n Elementen zu tun haben. Jede Berechnung, die mit einer analytischen Funktion durchgeführt wird, basiert auf einer aktuellen Zeile innerhalb einer Partition. Die aktuelle Zeile dient als Referenzpunkt, der den Anfang und das Ende des Fensters bestimmt. Beispielsweise könnte eine zentrierte gleitende Durchschnittsberechnung mit einem Fenster definiert werden, das die aktuelle Zeile, die sechs vorhergehenden Zeilen und die folgenden sechs Zeilen enthält. Dies würde ein Schiebefenster von 13 Reihen erzeugen (siehe Abbildung 22-2). Abbildung 22-2 Schiebefenster Beispiel Ranking, Windowing und Reporting Funktionen In diesem Abschnitt werden die grundlegenden analytischen Funktionen für Ranking, Fenstering und Reporting dargestellt. Beispiel Lineare Regressionsberechnung In diesem Beispiel berechnen wir eine Regressionsgerade, die die Menge eines Produktes als lineare Funktion des Produktlistenpreises ausdrückt. Die Berechnungen werden nach Vertriebskanal gruppiert. Die Werte SLOPE. INTCPT. RSQR sind Steigung, Intercept und Bestimmungskoeffizient der Regressionslinie. Der (ganzzahlige) Wert COUNT ist die Anzahl der Produkte in jedem Kanal, für den sowohl die verkauften Mengen als auch die Listenpreisdaten verfügbar sind. Statistische Aggregate Oracle bietet eine Reihe statistischer SQL-Funktionen und ein Statistikpaket, DBMSSTATFUNCS. In diesem Abschnitt werden einige der neuen Funktionen zusammen mit der grundlegenden Syntax aufgelistet. Beschreibende Statistik Sie können folgende deskriptive Statistik berechnen: Median eines Datensatz-Modus eines Datensatzes Sie können folgende parametrische Statistik berechnen: Spearmans rho Koeffizient Kendalls tau-b Koeffizient Zusätzlich zu den Funktionen verfügt diese Version über ein PLSQL-Paket, DBMSSTATFUNCS . Es enthält die beschreibende statistische Funktion ZUSAMMENFASSUNG zusammen mit Funktionen zur Unterstützung der Verteilungsanpassung. Die SUMMARY-Funktion fasst eine numerische Spalte einer Tabelle mit einer Vielzahl von deskriptiven Statistiken zusammen. Die fünf Verteilungsanpassungsfunktionen unterstützen normale, einheitliche, Weibull-, Poisson - und Exponentialverteilungen. Benutzerdefinierte Aggregate Oracle bietet eine Möglichkeit zur Erstellung eigener Funktionen, sogenannte benutzerdefinierte Aggregatfunktionen. Diese Funktionen sind in Programmiersprachen wie PLSQL, Java und C geschrieben und können als analytische Funktionen oder Aggregate in materialisierten Sichten verwendet werden. Weitere Informationen zu Syntax und Einschränkungen finden Sie im Oracle Database Data Cartridge Developers Guide. Die Vorteile dieser Funktionen sind: Sehr komplexe Funktionen können mit einer vollständig prozeduralen Sprache programmiert werden. Höhere Skalierbarkeit als andere Techniken, wenn benutzerdefinierte Funktionen für die Parallelverarbeitung programmiert werden. Objektdatentypen können verarbeitet werden. Als einfaches Beispiel für eine benutzerdefinierte Aggregatfunktion ist die Skew-Statistik zu betrachten. Diese Berechnung mißt, wenn ein Datensatz eine schiefe Verteilung um seinen Mittelwert hat. Es wird Ihnen sagen, wenn ein Schwanz der Verteilung ist deutlich größer als die anderen. Wenn Sie ein benutzerdefiniertes Aggregat namens udskew erstellt und auf die Kreditlimitdaten des vorherigen Beispiels angewendet haben, können die SQL-Anweisung und die Ergebnisse wie folgt aussehen: Bevor Sie benutzerdefinierte Aggregatfunktionen erstellen, sollten Sie prüfen, ob Ihre Anforderungen erfüllt werden können In regelmäßigen SQL. Viele komplexe Berechnungen sind direkt in SQL möglich, insbesondere durch Verwendung des CASE-Ausdrucks. Der Aufenthalt mit regulärem SQL ermöglicht eine einfachere Entwicklung, und viele Abfrageoperationen sind in SQL bereits gut parallelisiert. Selbst das frühere Beispiel, die Skew-Statistik, kann mit Standard, wenn auch langwierig, SQL erstellt werden. Pivoting-Operationen Das von Business Intelligence-Abfragen zurückgegebene d ata ist oft am nützlichsten, wenn es in einem Kreuztabellenformat dargestellt wird. Mit der Pivotklausel der SELECT-Anweisung können Sie Querstabulationsabfragen schreiben, die Zeilen in Spalten umwandeln und dabei Daten in den Prozess der Rotation aggregieren. Pivoting ist eine Schlüsseltechnologie in Data Warehouses. Dabei transformieren Sie mehrere Zeilen der Eingabe in weniger und allgemein breitere Zeilen im Data Warehouse. Beim Drehen wird für jedes Element in der Pivotspaltenwerteliste ein Aggregationsoperator angewendet. Die Pivot-Spalte darf keinen beliebigen Ausdruck enthalten. Wenn Sie auf einen Ausdruck schwenken müssen, sollten Sie den Ausdruck in einer Ansicht vor dem PIVOT-Vorgang platzieren. Die grundlegende Syntax lautet wie folgt: Um die Verwendung von Pivoting zu veranschaulichen, erstellen Sie die folgende Ansicht als Grundlage für spätere Beispiele: Beispiel: Pivoting Die folgende Anweisung veranschaulicht ein typisches Pivot in der Kanalspalte: Beachten Sie, dass die Ausgabe vier neue Aliasspalten erstellt hat , DIREKTVERTRIEB. INTERNETSALES. KATALOGVERZEICHNIS. Und TELESALES. Eine für jeden der Schwenkwerte. Die Ausgabe ist eine Summe. Wenn kein Alias ​​angegeben ist, sind die Spaltenüberschriften die Werte der IN-Liste. Schwenken auf mehrere Spalten Sie können auf mehr als einer Spalte schwenken. Die folgende Anweisung veranschaulicht einen typischen Mehrfachspalten-Pivot: Beachten Sie, dass dieses Beispiel eine mehrspaltige IN-Liste mit Spaltenüberschriften angibt, die für die Übereinstimmung mit den IN-Listenmitgliedern ausgelegt sind. Pivot: Mehrere Aggregate Sie können mit mehreren Aggregaten schwenken, wie im folgenden Beispiel gezeigt: Beachten Sie, dass die Abfrage Spaltenüberschriften erstellt, indem die Pivotwerte (oder der Alias) mit dem Alias ​​der Aggregatfunktion plus einem Unterstrich verkettet werden. Unterscheidung von PIVOT-generierten Nulls aus Nullstellen in Quelldaten Sie können zwischen Nullwerten unterscheiden, die aus der Verwendung von PIVOT und denen, die in den Quelldaten vorhanden sind, erzeugt werden. Das folgende Beispiel veranschaulicht NULLs, die PIVOT generiert. Die folgende Abfrage gibt Zeilen mit 5 Spalten, Spalte prodid zurück. Und die sich ergebenden Säulen Q1. Q1COUNTTOTAL. Q2. Q2COUNTTOTAL. Für jeden eindeutigen Wert von prodid. Q1COUNTTOTAL gibt die Gesamtzahl der Zeilen zurück, deren qtr-Wert Q1 ist. Das heißt, und Q2COUNTTOTAL gibt die Gesamtzahl der Zeilen zurück, deren qtr-Wert Q2 ist. Angenommen, wir haben eine Tabelle sales2 der folgenden Struktur: Aus dem Ergebnis wissen wir, dass es für prodid 100 zwei Verkaufsreihen für Quartal Q1 gibt. Und 1 Verkaufsreihe für Quartal Q2 für Prodid 200 gibt es 1 Verkaufsreihe für Quartal Q1. Und keine Verkaufsreihe für Quartal Q2.So, in Q2COUNTTOTAL. Können Sie identifizieren, dass NULLlt1gt aus einer Zeile in der ursprünglichen Tabelle stammt, deren Maß einen Nullwert hat, während NULLlt2gt aufgrund der Tatsache, dass keine Zeile in der ursprünglichen Tabelle für prodid 200 in Quartal Q2 vorhanden ist. Unpivoting-Operationen Ein Unpivot kehrt einen PIVOT-Vorgang nicht um. Stattdessen dreht es Daten aus Spalten in Zeilen. Wenn Sie mit geschwenkten Daten arbeiten, kann eine UNPIVOT-Operation keine Aggregationen, die von PIVOT oder auf andere Weise erstellt wurden, umkehren. Um Unpivot zu veranschaulichen, erstellen Sie zunächst eine schwenkbare Tabelle, die vier Spalten enthält, für Quartale des Jahres: Die Tabelleninhalte ähneln den folgenden: Die folgende UNPIVOT-Operation dreht die Viertelspalten in Zeilen. Für jedes Produkt gibt es vier Zeilen, eine für jedes Quartal. Beachten Sie die Verwendung von INCLUDE NULLS in diesem Beispiel. Sie können auch EXKLUSIVE NULLS verwenden. Die die Standardeinstellung ist. Darüber hinaus können Sie auch mit zwei Spalten unpivot, wie im Folgenden: Wildcard und Unterabfrage Pivoting mit XML-Operationen Wenn Sie ein Platzhalter-Argument oder Unterabfrage in Ihre Pivot-Spalten verwenden möchten, können Sie dies mit PIVOT-XML-Syntax. Mit PIVOT XML ist die Ausgabe der Operation korrekt formatiertes XML. Das folgende Beispiel veranschaulicht die Verwendung des Wildcard-Schlüsselworts ANY. Es gibt XML aus, das alle Channel-Werte in salesview enthält: Beachten Sie, dass das Schlüsselwort ANY in PIVOT-Operationen nur als Teil einer XML-Operation verfügbar ist. Diese Ausgabe enthält Daten für Fälle, in denen der Kanal im Datensatz vorhanden ist. Beachten Sie außerdem, dass Aggregationsfunktionen eine GROUP BY-Klausel angeben müssen, um mehrere Werte zurückzugeben, doch enthält die Pivotklausel keine explizite GROUP BY-Klausel. Stattdessen führt die Pivotklausel eine implizite GROUP BY durch. Das folgende Beispiel veranschaulicht die Verwendung einer Unterabfrage. Es gibt XML aus, das alle Kanalwerte und die Verkaufsdaten enthält, die jedem Kanal entsprechen: Der Ausgang verdichtet die Daten, um alle möglichen Kanäle für jedes Produkt einzuschließen. Data Densification for Reporting Daten werden in der Regel in spärlicher Form gespeichert. Das heißt, wenn kein Wert für eine gegebene Kombination von Dimensionswerten existiert, existiert keine Zeile in der Faktentabelle. Sie können die Daten jedoch in dichter Form anzeigen, wobei Zeilen für alle Kombinationen von Bemaßungswerten angezeigt werden, auch wenn keine Faktendaten für sie vorhanden sind. Wenn beispielsweise ein Produkt während eines bestimmten Zeitraums nicht verkauft wurde, können Sie das Produkt weiterhin für diesen Zeitraum sehen, wenn der Verkaufswert daneben liegt. Darüber hinaus können Zeitreihenberechnungen am einfachsten durchgeführt werden, wenn Daten entlang der Zeitdimension dicht sind. Dies liegt daran, dass dichte Daten eine konsistente Anzahl von Zeilen für jede Periode füllen, was wiederum es einfach macht, die analytischen Fensterfunktionen mit physischen Offsets zu verwenden. Datenverdichtung ist der Prozess der Umwandlung von spärlichen Daten in dichte Form. Um das Problem der Sparsität zu überwinden, können Sie eine partitionierte äußere Verknüpfung verwenden, um die Lücken in einer Zeitreihe oder einer anderen Dimension zu füllen. Eine solche Verknüpfung erweitert die herkömmliche äußere Verknüpfungssyntax, indem die äußere Verknüpfung auf jede in einer Abfrage definierte logische Partition angewendet wird. Oracle partitioniert die Zeilen in Ihrer Abfrage auf der Grundlage des in der PARTITION BY-Klausel angegebenen Ausdrucks logisch. Das Ergebnis einer partitionierten äußeren Verknüpfung ist eine UNION der äußeren Verknüpfungen jeder der Partitionen in der logisch partitionierten Tabelle mit der Tabelle auf der anderen Seite der Verknüpfung. Beachten Sie, dass Sie diese Art von Join verwenden können, um die Lücken in jeder Dimension zu füllen, nicht nur die Zeitdimension. Die meisten Beispiele beziehen sich hier auf die Zeitdimension, da sie die am häufigsten verwendete Dimension für Vergleiche ist. Partition Join Syntax Die Syntax für partitionierte äußere Verknüpfung erweitert die ANSI SQL JOIN-Klausel um den Ausdruck PARTITION BY, gefolgt von einer Ausdrucksliste. Die Ausdrücke in der Liste geben die Gruppe an, auf die die äußere Verknüpfung angewendet wird. Im Folgenden finden Sie die beiden Syntaxformen, die normalerweise für partitionierte äußere Verknüpfungen verwendet werden: Beachten Sie, dass FULL OUTER JOIN nicht mit einer partitionierten äußeren Verknüpfung unterstützt wird. Beispiel von Sparse-Daten Eine typische Situation mit einer spärlichen Dimension wird im folgenden Beispiel gezeigt, das die wöchentlichen Verkäufe und den Jahresabschluss für das Produkt Bounce für die Wochen 20-30 in den Jahren 2000 und 2001 berechnet Erwarten würde 22 Reihen von Daten (11 Wochen jeweils von 2 Jahren), wenn die Daten waren dicht. Allerdings erhalten wir nur 18 Zeilen, weil die Wochen 25 und 26 im Jahr 2000 und die Wochen 26 und 28 im Jahr 2001 fehlen. Füllen von Datenlücken Wir können die spärlichen Daten der vorherigen Abfrage nehmen und eine partitionierte äußere Verknüpfung mit einem dichten Satz ausführen Zeit-Daten. In der folgenden Abfrage übergeben wir unsere ursprüngliche Abfrage als v und wir wählen Daten aus der Zeittabelle, die wir alias als t. Hier werden 22 Zeilen abgerufen, da es keine Lücken in der Reihe gibt. Die vier hinzugefügten Zeilen haben jeweils 0, wenn der Verkaufswert auf 0 gesetzt wird, indem die NVL-Funktion verwendet wird. Beachten Sie, dass in dieser Abfrage eine WHERE-Bedingung für Wochen zwischen 20 und 30 in der Inline-Ansicht für die Zeitdimension platziert wurde. Dies wurde eingeführt, um die Ergebnismenge klein zu halten. Füllen von Lücken in zwei Dimensionen N-dimensionale Daten werden typischerweise als ein dichter zweidimensionaler Querstreifen von (n - 2) Seitenabmessungen angezeigt. Dies erfordert, dass alle Bemaßungswerte für die beiden Dimensionen, die in der Quer-Registerkarte angezeigt werden, eingegeben werden. Im Folgenden ist ein anderes Beispiel, bei dem die partitionierte äußere Verknüpfungsfunktion zum Füllen der Lücken auf zwei Dimensionen verwendet werden kann: In dieser Abfrage die WITH-Unterabfrage Factoring-Klausel V1 fasst die Umsatzdaten auf Produkt-, Länder - und Jahresebene zusammen. Dieses Ergebnis ist spärlich, aber Benutzer können alle Länder-, Jahr-Kombinationen für jedes Produkt sehen. Um dies zu erreichen, nehmen wir jede Partition von v1 basierend auf Produktwerten und äußere verbinden sie auf der Land-Dimension zuerst. Dieses gibt uns alle Werte des Landes für jedes Produkt. Wir nehmen dann dieses Ergebnis und partitionieren es auf Produkt-und Landeswerte und dann äußere verbinden sie auf Zeitdimension. Damit erhalten Sie für jede Produkt - und Länderkombination alle Zeitwerte. Füllen von Lücken in einer Inventartabelle In einer Inventartabelle wird typischerweise eine Menge von Einheiten angezeigt, die für verschiedene Produkte verfügbar sind. Diese Tabelle ist spärlich: sie speichert nur eine Zeile für ein Produkt, wenn es ein Ereignis gibt. Für eine Verkaufstabelle ist die Veranstaltung ein Verkauf, und für die Inventartabelle ist das Ereignis eine Mengenänderung, die für ein Produkt verfügbar ist. Beachten Sie zum Beispiel die folgende Inventartabelle: Die Inventartabelle verfügt nun über die folgenden Zeilen: Für Berichtszwecke können Benutzer diese Inventardaten anders sehen. Sie können z. B. alle Werte der Zeit für jedes Produkt sehen. Dies kann durch partitionierte äußere Verknüpfung erreicht werden. Außerdem können Benutzer für die neu eingefügten Zeilen fehlender Zeitperioden die Werte für Mengeneinheitsspalten sehen, die von der aktuellsten vorhandenen Zeitdauer übertragen werden sollen. Letzteres kann mit dem analytischen Fensterfunktion-LASTVALUE-Wert erreicht werden. Hier ist die Abfrage und die gewünschte Ausgabe: Die innere Abfrage berechnet eine partitionierte äußere Verknüpfung auf Zeit innerhalb jedes Produkts. Die innere Abfrage verdichtet die Daten auf der Zeitdimension (dh die Zeitdimension hat nun eine Zeile für jeden Wochentag). Für die neu hinzugefügten Zeilen hat die Maßsäulenmenge jedoch Nullen (siehe die Ausgabe in der Spaltenmenge in den folgenden Ergebnissen: Die äußere Abfrage verwendet die analytische Funktion LASTVALUE.) Bei Anwendung dieser Funktion werden die Daten nach Produkt partitioniert und die Daten auf der (Timeid) Für jede Zeile findet die Funktion aufgrund der Option IGNORE NULLS, die Sie sowohl mit LASTVALUE als auch mit FIRSTVALUE verwenden können, den letzten Nicht-Nullwert im Fenster. In der Spalte repequentity sehen wir die gewünschte Ausgabe Die folgende Ausgabe: Berechnen von Datenwerten zum Füllen von Lücken Beispiele im vorherigen Abschnitt veranschaulichen, wie partitionierte äußere Verknüpfung verwendet wird, um Lücken in einer oder mehreren Dimensionen zu füllen. Die Ergebnismengen, die durch partitionierte äußere Verknüpfung erzeugt werden, haben jedoch Nullwerte für Spalten, die nicht in enthalten sind Die PARTITION BY-Liste, die normalerweise analytische SQL-Funktionen verwenden, um diese Nullwerte durch einen Nicht-Nullwert zu ersetzen. Beispielsweise berechnet die folgende Abfrage monatliche Summen für Produkte 64 MB Speicherkarte und DVD-R Discs (Produkt-IDs 122 und 136) für das Jahr 2000. Es verwendet partitionierte äußere Verknüpfung, um Daten für alle Monate zu verdichten. Für die fehlenden Monate wird dann die analytische SQL-Funktion AVG verwendet, um die Umsätze und Einheiten als den Durchschnitt der Monate zu errechnen, in denen das Produkt verkauft wurde. Bei der Arbeit in SQLPlus enthalten die folgenden zwei Befehle die Spaltenüberschriften für eine bessere Lesbarkeit der Ergebnisse: Zeitreihenberechnungen für Densified Data Densificatio n ist nicht nur für Berichtszwecke gedacht. Es ermöglicht auch bestimmte Berechnungsarten, insbesondere Zeitreihenberechnungen. Zeitreihenberechnungen sind einfacher, wenn Daten entlang der Zeitdimension dicht sind. Dichte Daten weisen eine konsistente Anzahl von Zeilen für jede Zeitperiode auf, die es wiederum einfach macht, analytische Fensterfunktionen mit physischen Offsets zu verwenden. Um dies zu illustrieren, nehmen wir zunächst das Beispiel zum Füllen von Lücken in Daten. Und fügen Sie eine analytische Funktion zu dieser Abfrage hinzu. In der folgenden erweiterten Version berechnen wir wöchentlich jährliche Verkäufe neben dem wöchentlichen Verkauf. Die NULL-Werte, die die partitionierte äußere Verknüpfung bei der Herstellung der Zeitreihen dicht einfügt, werden auf die übliche Weise behandelt: Die SUM-Funktion behandelt sie als 0s. Period-to-Period-Vergleich für eine Zeitstufe: Beispiel Wie verwenden wir diese Funktion, um Werte über Zeiträume hinweg zu vergleichen. Im Einzelnen: Wie berechnen wir einen Vergleichsvergleich im Vergleich zum Vorjahr auf der Wochesebene Die folgende Abfrage gibt dieselbe Zeile zurück , Für jedes Produkt den Jahresabschluss für jede Woche von 2001 mit dem von 2000. Beachten Sie, dass wir in diesem Beispiel mit einer WITH-Klausel beginnen. Dies verbessert die Lesbarkeit der Abfrage und lässt uns auf den partitionierten äußeren Join fokussieren. Bei der Arbeit in SQLPlus enthält der folgende Befehl die Spaltenüberschriften für eine bessere Lesbarkeit der Ergebnisse: In der FROM-Klausel der Inline-Ansicht densesales. Verwenden wir eine partitionierte äußere Verknüpfung der Aggregatansicht v und der Zeitansicht t, um Lücken in den Verkaufsdaten entlang der Zeitdimension zu füllen. Die Ausgabe der partitionierten äußeren Verknüpfung wird dann durch die analytische Funktion SUM verarbeitet. OVER, um den wöchentlichen Jahresabschluss zu berechnen (die weeklyytdsales Spalte). So berechnet die Ansicht densesales die Jahresabschlussdaten für jede Woche, einschließlich der fehlenden Summenansichten. Die Inline-Ansicht yearoveryearsales berechnet dann den vorjährigen wöchentlichen Jahresabschluss mit der LAG-Funktion. Die LAG-Funktion weeklyytdsalesprioryear gibt eine PARTITION BY-Klausel an, die Zeilen für die gleiche Woche der Jahre 2000 und 2001 in einer einzigen Partition paaren. Wir übergeben dann einen Offset von 1 an die LAG-Funktion, um das wöchentliche Jahr zu erhalten, um die Verkäufe für das vorherige Jahr zu erreichen. Der äußerste Abfrageblock wählt Daten von yearoveryearsales mit der Bedingung yr 2001. und so gibt die Abfrage für jedes Produkt seine wöchentliche zurück Jahr-to-date-Umsatz in den angegebenen Wochen der Jahre 2001 und 2000. Period-to-Period Vergleich für mehrere Zeitstufen: Beispiel Während das vorherige Beispiel zeigt uns einen Weg, um Vergleiche für ein einziges Zeitniveau zu schaffen, wäre es noch mehr Nützlich, um mehrere Zeitniveaus in einer einzelnen Abfrage zu behandeln. Zum Beispiel könnten wir den Umsatz gegenüber dem Vorjahreszeitraum auf Jahr, Quartal, Monat und Tagestufe vergleichen. Wie können wir eine Abfrage erstellen, die einen jährlichen Vergleich des Jahresabschlusses für alle Ebenen unserer Zeithierarchie durchführt Wir werden mehrere Schritte zur Ausführung dieser Aufgabe durchführen. Das Ziel ist eine einzelne Abfrage mit Vergleichen am Tag, Woche, Monat, Quartal und Jahr. Die Schritte sind wie folgt: Wir erstellen eine Ansicht namens cubeprodtime. Die einen hierarchischen Würfel von Umsatz aggregiert über Zeiten und Produkte hält. Dann erstellen wir eine Ansicht der Zeitdimension, die als Kante des Würfels verwendet werden soll. Die Zeitkante, die einen vollständigen Satz von Daten enthält, wird partitioniert mit den spärlichen Daten in der View cubeprodtime verbunden. Schließlich, für maximale Leistung, erstellen wir eine materialisierte Ansicht, mvprodtime. Gebaut mit der gleichen Definition wie cubeprodtime. Weitere Informationen zu hierarchischen Cubes finden Sie in Kapitel 21, SQL für Aggregation in Data Warehouses. Die materialisierte Ansicht wird in Schritt 1 im folgenden Abschnitt definiert. Schritt 1 Erstellen Sie die hierarchische Cube-Ansicht Die materialisierte Ansicht, die im Folgenden dargestellt wird, kann in Ihrem System vorhanden sein, wenn nicht, erstellen Sie sie jetzt. Wenn Sie es generieren müssen, beachten Sie, dass wir die Abfrage auf nur zwei Produkte beschränken, um die Bearbeitungszeit kurz zu halten: Da diese Ansicht auf zwei Produkte beschränkt ist, gibt sie knapp über 2200 Zeilen zurück. Beachten Sie, dass die Spalte HierarchicalTime Stringdarstellungen von Zeit aus allen Ebenen der Zeithierarchie enthält. Der CASE-Ausdruck, der für die HierarchicalTime-Spalte verwendet wird, fügt eine Markierung (0, 1.) jeder Datumszeichenfolge hinzu, um die Zeitstufe des Wertes anzugeben. A 0 steht für das Jahr, 1 für Quartale, 2 für Monate und 3 für Tag. Beachten Sie, dass die GROUP BY-Klausel eine verkettete ROLLUP ist, die die Rollup-Hierarchie für die Zeit - und Produktdimensionen angibt. Die GROUP BY-Klausel bestimmt den hierarchischen Cube-Inhalt. Schritt 2 Erstellen Sie die Ansicht edgetime, die eine vollständige Menge von Datumswerten ist Edgetime ist die Quelle für das Ausfüllen von Zeitlücken im hierarchischen Cube mit einem partitionierten äußeren Join. Die Spalte HierarchicalTime in edgetime wird in einer partitionierten Verknüpfung mit der Spalte HierarchicalTime in der View cubeprodtime verwendet. Die folgende Anweisung definiert edgetime: Schritt 3 Erstellen der materialisierten Ansicht mvprodtime zur Unterstützung schnellerer Performance Die materialisierte Viewdefinition ist ein Duplikat der zuvor definierten View cubeprodtime. Da es sich um eine doppelte Abfrage handelt, werden Verweise auf cubeprodtime neu geschrieben, um die materialisierte Ansicht mvprodtime zu verwenden. Die folgenden Materialisierungen können in Ihrem System vorhanden sein, wenn nicht, erstellen Sie es jetzt. Wenn Sie es generieren müssen, beachten Sie, dass wir die Abfrage auf nur zwei Produkte beschränken, um die Bearbeitungszeit kurz zu halten. Schritt 4 Erstellen der Vergleichsabfrage Wir haben nun die Stufe für unsere Vergleichsabfrage gesetzt. Wir können Periodenvergleichsberechnungen auf allen Zeitniveaus erhalten. Es erfordert das Anwenden analytischer Funktionen auf einen hierarchischen Würfel mit dichten Daten entlang der Zeitdimension. Einige der Berechnungen, die wir für jedes Zeitniveau erreichen können, sind: Summe der Verkäufe für vorherigen Zeitraum auf allen Zeitniveaus. Umsatzabweichung gegenüber Vorjahr. Summe des Umsatzes im gleichen Zeitraum vor einem Jahr auf allen Ebenen der Zeit. Umsatzveränderung im Vergleich zum Vorjahreszeitraum. Im folgenden Beispiel werden alle vier Berechnungen durchgeführt. Es verwendet eine partitionierte äußere Verknüpfung der Ansichten cubeprodtime und edgetime, um eine Inline-Ansicht von dichten Daten namens densecubeprodtime zu erstellen. Die Abfrage verwendet dann die LAG-Funktion auf dieselbe Weise wie das vorherige einstufige Beispiel. Die äußere WHERE-Klausel gibt die Zeit auf drei Ebenen an: die Tage August 2001, den gesamten Monat und das gesamte dritte Quartal 2001. Beachten Sie, dass die letzten beiden Zeilen der Ergebnisse die Monats - und Quartalsniveau-Aggregationen enthalten Die Ergebnisse leichter zu lesen, wenn Sie SQLPlus verwenden, sollten die Spaltenüberschriften mit den folgenden Befehlen angepasst werden. Die Kommandos falten die Spaltenüberschriften, um die Zeilenlänge zu reduzieren: Hier ist die Abfrage, die die aktuellen Verkäufe mit dem Vorjahresvergleich verglichen hat: Die erste LAG-Funktion (salespriorperiod) partitioniert die Daten auf gidp. Katze. Subkat. Prod. Gidt und ordnet die Zeilen auf allen Zeitdimensionsspalten an. Es erhält den Verkaufswert der Vorperiode, indem er einen Offset von 1 übergibt. Die zweite LAG-Funktion (salessameperiodprioryear) partitioniert die Daten auf zusätzlichen Spalten qtrnum. Monnum Und daynum und ordnet es auf yr, damit, mit einem Versatz von 1, es den Vorjahresabsatz für den gleichen Zeitraum berechnen kann. Die äußerste SELECT-Klausel berechnet die Abweichungen. Erstellen eines benutzerdefinierten Member in einer Dimension: Beispiel In vielen analytischen SQL-Tasks ist es hilfreich, benutzerdefinierte Member in einer Dimension zu definieren. Beispielsweise können Sie einen spezialisierten Zeitraum für Analysen definieren. Sie können eine partitionierte äußere Verknüpfung verwenden, um ein Element vorübergehend zu einer Dimension hinzuzufügen. Beachten Sie, dass die neue SQL-MODEL-Klausel geeignet ist, komplexere Szenarios mit neuen Mitgliedern in Dimensionen zu erstellen. Weitere Informationen zu diesem Thema finden Sie in Kapitel 23, SQL für Modellierung. Als Beispiel für eine Aufgabe, was ist, wenn wir ein neues Mitglied für unsere Zeitdimension definieren wollen Wir wollen in unserer Zeitdimension ein 13. Mitglied der Monatsebene schaffen. Dieser 13. Monat ist definiert als die Summe der Verkäufe für jedes Produkt im ersten Monat eines jeden Quartals des Jahres 2001. Die Lösung hat zwei Schritte. Beachten Sie, dass wir diese Lösung mit den Ansichten und Tabellen erstellen, die im vorherigen Beispiel erstellt wurden. Es sind zwei Schritte erforderlich. Erstellen Sie zunächst eine Ansicht mit dem neuen Element, das der entsprechenden Dimension hinzugefügt wurde. Die Ansicht verwendet eine Operation UNION ALL, um das neue Element hinzuzufügen. Verwenden Sie einen CASE-Ausdruck und eine partitionierte äußere Verknüpfung, um das benutzerdefinierte Mitglied abzufragen. Unser neues Mitglied für die Zeitdimension wird mit folgender Ansicht erstellt: In dieser Anweisung wird die Ansicht timec definiert, indem eine UNION ALL der Edgetime-Ansicht (definiert im vorherigen Beispiel) und der benutzerdefinierte 13. Monat ausgeführt wird. Der Gidt-Wert von 8 wurde gewählt, um das benutzerdefinierte Mitglied von den Standardmitgliedern zu unterscheiden. Die UNION ALL gibt die Attribute für ein 13. Monatsmitglied an, indem ein SELECT aus der DUAL-Tabelle ausgeführt wird. Beachten Sie, dass die Gruppierungs-ID Spalte GIDT. Ist auf 8 gesetzt und die Viertelzahl auf 5 gesetzt. Dann ist der zweite Schritt, eine Inline-Ansicht der Abfrage zu verwenden, um eine partitionierte äußere Verknüpfung von cubeprodtime mit timec auszuführen. Dieser Schritt erzeugt Umsatzdaten für den 13. Monat auf jeder Ebene der Produktaggregation. In der Hauptabfrage wird die analytische Funktion SUM mit einem CASE-Ausdruck verwendet, um den 13. Monat zu berechnen, der als Summierung des ersten Monatsumsatzes jedes Quartals definiert ist. Die SUM-Funktion verwendet einen CASE, um die Daten auf Monate 1, 4, 7 und 10 innerhalb eines Jahres zu begrenzen. Aufgrund des winzigen Datensatzes mit nur 2 Produkten sind die Rollup-Werte der Ergebnisse notwendigerweise Wiederholungen von Aggregationen auf niedrigerer Ebene. Für realistischere Rollup-Werte können Sie weitere Produkte aus den Spielkonsolen - und Y-Box-Spiele-Unterkategorien in die darunterliegende materialisierte Ansicht aufnehmen. Verschiedene Analyse - und Reporting-Funktionen Dieser Abschnitt erläutert die folgenden zusätzlichen analytischen Fähigkeiten: WIDTHBUCKET Funktion Für einen gegebenen Ausdruck gibt die WIDTHBUCKET-Funktion die Bucket-Nummer zurück, nach der das Ergebnis dieses Ausdrucks zugewiesen wird. Sie können mit dieser Funktion Gleichheitshistogramme erzeugen. Equiwidth-Histogramme unterteilen Datensätze in Buckets, deren Intervallgröße (höchster Wert zum niedrigsten Wert) gleich ist. Die Anzahl der Zeilen, die von jedem Eimer gehalten werden, variiert. Eine verwandte Funktion, NTILE. Schafft gleich große Schaufeln. Equiwidth-Histogramme können nur für numerische, Datums - oder Datetime-Typen erzeugt werden. Die ersten drei Parameter sollten also alle numerischen Ausdrücke oder alle Datumsausdrücke sein. Andere Ausdrücke sind nicht zulässig. Wenn der erste Parameter NULL ist. Das Ergebnis ist NULL. Wenn der zweite oder dritte Parameter NULL ist. Wird eine Fehlermeldung zurückgegeben, da ein NULL-Wert keinen Endpunkt (oder einen beliebigen Punkt) für einen Bereich in einer Datums - oder numerischen Wertdimension angibt. Der letzte Parameter (Anzahl der Buckets) sollte ein numerischer Ausdruck sein, der einen positiven Integerwert 0, NULL auswertet. Oder ein negativer Wert führt zu einem Fehler. Die Buckets sind von 0 bis (n 1) nummeriert. Bucket 0 enthält die Anzahl der Werte, die kleiner als das Minimum sind. Bucket (n 1) hält die Anzahl der Werte, die größer oder gleich dem maximalen angegebenen Wert sind. WIDTHBUCKET Syntax Das WIDTHBUCKET nimmt vier Ausdrücke als Parameter an. Der erste Parameter ist der Ausdruck, für den das equiwidth-Histogramm gilt. Der zweite und dritte Parameter sind Ausdrücke, die die Endpunkte des akzeptablen Bereichs für den ersten Parameter bezeichnen. Der vierte Parameter bezeichnet die Anzahl der Buckets. Betrachten Sie die folgenden Daten von Tischkunden. Dass die Kreditlimiten von 17 Kunden zeigt. Diese Daten werden in der Abfrage, die in Beispiel 22-24 gezeigt wird, gesammelt. In der Tabelle Kunden. Die Spalte custcreditlimit enthält Werte zwischen 1500 und 15000, und wir können die Werte zu vier Equiwidth Buckets, nummeriert von 1 bis 4, mit WIDTHBUCKET (custcreditlimit, 0, 20000, 4) zuweisen. Idealerweise ist jede Schaufel ein geschlossenes Intervall der reellen Zahlenlinie, z. B. ist die Schaufelzahl 2 Scores zwischen 5000.0000 und 9999.9999 zugeordnet. Manchmal mit 5000, 10000 bezeichnet), um anzuzeigen, daß 5.000 in dem Intervall enthalten sind und 10.000 ausgeschlossen sind. Um Werte außerhalb des Bereiches 0, 20.000 zu erfassen, werden Werte kleiner als 0 einer bezeichneten Unterlaufschaufel mit der Nummer 0 und Werte größer oder gleich 20.000 einer vorgegebenen Überlaufschaufel zugeordnet, die mit 5 numeriert ist (num Schaufeln 1 Im Algemeinen). See Figure 22-3 for a graphical illustration of how the buckets are assigned. Figure 22-3 Bucket Assignments You can specify the bounds in the reverse order, for example, WIDTHBUCKET ( custcreditlimit. 20000. 0. 4 ). When the bounds are reversed, the buckets will be open-closed intervals. In this example, bucket number 1 is ( 15000,20000 , bucket number 2 is ( 10000,15000 , and bucket number 4, is ( 0 ,5000 . The overflow bucket will be numbered 0 ( 20000. infinity ), and the underflow bucket will be numbered 5 (- infinity. 0 . It is an error if the bucket count parameter is 0 or negative. Example 22-24 WIDTHBUCKET The followin g query shows the bucket numbers for the credit limits in the customers table for both cases where the boundaries are specified in regular or reverse order. We use a range of 0 to 20,000. Linear Algebra Linear algebra is a branch of mathematics with a wide range of practical applications. Many areas have tasks that can be expressed using linear algebra, and here are some examples from several fields: statistics (multiple linear regression and principle components analysis), data mining (clustering and classification), bioinformatics (analysis of microarray data), operations research (supply chain and other optimization problems), econometrics (analysis of consumer demand data), and finance (asset allocation problems). Various libraries for linear algebra are freely available for anyone to use. Oracles UTLNLA package exposes matrix PLSQL data types and wrapper PLSQL subprograms for two of the most popular and robust of these libraries, BLAS and LAPACK. Linear algebra depends on matrix manipulation. Performing matrix manipulation in PLSQL in the past required inventing a matrix representation based on PLSQLs native data types and then writing matrix manipulation routines from scratch. This required substantial programming effort and the performance of the resulting implementation was limited. If developers chose to send data to external packages for processing rather than create their own routines, data transfer back and forth could be time consuming. Using the UTLNLA package lets data stay within Oracle, removes the programming effort, and delivers a fast implementation. Example 22-25 Linear Algebra Here is an example of how Oracles linear algebra support could be used for business analysis. It invokes a multiple linear regression application built using the UTLNLA package. The multiple regression application is implemented in an object called OLSRegression. Note that sample files for the OLS Regression object can be found in ORACLEHOMEplsqldemo . Consider the scenario of a retailer analyzing the effectiveness of its marketing program. Each of its stores allocates its marketing budget over the following possible programs: media advertisements ( media ), promotions ( promo ), discount coupons ( disct ), and direct mailers ( dmail ). The regression analysis builds a linear relationship between the amount of sales that an average store has in a given year ( sales ) and the spending on the four components of the marketing program. Suppose that the marketing data is stored in the following table: Then you can build the following sales-marketing linear model using coefficients: This model can be implemented as the following view, which refers to the OLS regression object: Using this view, a marketing program manager can perform an analysis such as Is this sales-marketing model reasonable for year 2004 data That is, is the multiple-correlation greater than some acceptable value, say, 0.9 The SQL for such a query might be as follows: You could also solve questions such as What is the expected base-line sales revenue of a store without any marketing programs in 2003 or Which component of the marketing program was the most effective in 2004 That is, a dollar increase in which program produced the greatest expected increase in sales See Oracle Database PLSQL Packages and Types Reference for further information regarding the use of the UTLNLA package and linear algebra. CASE Expressions Oracle now supports simple and searched CASE statements. CASE statements are similar in purpose to the DECODE statement, but they offer more flexibility and logical power. They are also easier to read than traditional DECODE statements, and offer better performance as well. They are commonly used when breaking categories into buckets like age (for example, 20-29, 30-39, and so on). The syntax for simple CASE statements is: Simple CASE expressions test if the expr value equals the comparisonexpr . The syntax for searched CASE statements is: You can use any kind of condition in a searched CASE expression, not just an equality test. You can specify only 65,535 arguments and each WHEN. THEN pair counts as two arguments. To avoid exceeding this limit, you can nest CASE expressions so that the returnexpr itself is a CASE expression. Example 22-26 CASE Suppose you wanted to find the average salary of all employees in the company. If an employees salary is less than 2000, you want the query to use 2000 instead. Without a CASE statement, you might choose to write this query as follows: Note that this runs against the hr sample schema. In this, foo is a function that returns its input if the input is greater than 2000, and returns 2000 otherwise. The query has performance implications because it needs to invoke a function for each row. Writing custom functions can also add to the development load. Using CASE expressions in the database without PLSQL, this query can be rewritten as: Using a CASE expression lets you avoid developing custom functions and can also perform faster. Example 22-27 CASE for Aggregating Independent Subsets Using CASE inside aggregate functions is a convenient way to perform aggregates on multiple subsets of data when a plain GROUP BY will not suffice. For instance, the preceding example could have included multiple AVG columns in its SELECT list, each with its own CASE expression. We might have had a query find the average salary for all employees in the salary ranges 0-2000 and 2000-5000. It would look like: Although this query places the aggregates of independent subsets data into separate columns, by adding a CASE expression to the GROUP BY clause we can display the aggregates as the rows of a single column. The next section shows the flexibility of this approach with two approaches to creating histograms with CASE . Creating Histograms You can use the CASE statement when you want to obtain histograms with user-defined buckets (both in number of buckets and width of each bucket). The following are two examples of histograms created with CASE statements. In the first example, the histogram totals are shown in multiple columns and a single row is returned. In the second example, the histogram is shown with a label column and a single column for totals, and multiple rows are returned. Example 22-28 Histogram Example 1 Example 22-29 Histogram Example 2 Frequent Itemsets Instead of counting how often a given event occurs (for example, how often someone has purchased milk at the grocery), you may find it useful to count how often multiple events occur together (for example, how often someone has purchased both milk and cereal together at the grocery store). You can count these multiple events using what is called a frequent itemset, which is, as the name implies, a set of items. Some examples of itemsets could be all of the products that a given customer purchased in a single trip to the grocery store (commonly called a market basket), the web pages that a user accessed in a single session, or the financial services that a given customer utilizes. The practical motivation for using a frequent itemset is to find those itemsets that occur most often. If you analyze a grocery stores point-of-sale data, you might, for example, discover that milk and bananas are the most commonly bought pair of items. Frequent itemsets have thus been used in business intelligence environments for many years, with the most common one being for market basket analysis in the retail industry. Frequent itemset calculations are integrated with the database, operating on top of relational tables and accessed through SQL. This integration provides the following key benefits: Applications that previously relied on frequent itemset operations now benefit from significantly improved performance as well as simpler implementation. SQL-based applications that did not previously use frequent itemsets can now be easily extended to take advantage of this functionality. Frequent itemsets analysis is performed with the PLSQL package DBMSFREQUENTITEMSETS. See Oracle Database PLSQL Packages and Types Reference for more information. In addition, there is an example of frequent itemset usage in Frequent itemsets . Scripting on this page enhances content navigation, but does not change the content in any way. Most people are familiar with the phrase, quotthis will kill two birds with one stonequot. If you39re not, the phase refers to an approach that addresses two objectives in one action. (Unfortunately, the expression itself is rather unpleasant, as most of us don39t want to throw stones at innocent animals) Today I39m going to cover some basics on two great features in SQL Server: the Columnstore index (available only in SQL Server Enterprise) and the SQL Query Store . Microsoft actually implemented the Columnstore index in SQL 2012 Enterprise, though they39ve enhanced it in the last two releases of SQL Server. Microsoft introduced the Query Store in SQL Server 2016. So, what are these features and why are they important Well, I have a demo that will introduce both features and show how they can help us. Before I go any further, I also cover this (and other SQL 2016 features) in my CODE Magazine article on new features SQL 2016. As a basic introduction, the Columnstore index can help speed up queries that scanaggregate over large amounts of data, and the Query Store tracks query executions, execution plans, and runtime statistics that you39d normally need to collect manually. Trust me when I say, these are great features. For this demo, I39ll be using the Microsoft Contoso Retail Data Warehouse demo database. Loosely speaking, Contoso DW is like quota really big AdventureWorksquot, with tables containing millions of rows. (The largest AdventureWorks table contains roughly 100,000 rows at most). You can download the Contoso DW database here: microsoften-usdownloaddetails. aspxid18279. Contoso DW works very well when you want to test performance on queries against larger tables. Contoso DW contains a standard data warehouse Fact table called FactOnLineSales, with 12.6 million rows. That39s certainly not the largest data warehouse table in the world, but it39s not child39s play either. Suppose I want to summarize product sales amount for 2009, and rank the products. I might query the fact table and join to the Product Dimension table and use a RANK function, like so: Here39s a partial result set of the top 10 rows, by Total Sales. On my laptop (i7, 16 GB of RAM), the query takes anywhere from 3-4 seconds to run. That might not seem like the end of the world, but some users might expect near-instant results (the way you might see near-instant results when using Excel against an OLAP cube). The only index I currently have on this table is a clustered index on a sales key. If I look at the execution plan, SQL Server makes a suggestion to add a covering index to the table: Now, just because SQL Server suggests an index, doesn39t mean you should blindly create indexes on every quotmissing indexquot message. However, in this instance, SQL Server detects that we are filtering based on year, and using the Product Key and Sales Amount. So, SQL Server suggests a covering index, with the DateKey as the index key field. The reason we call this a quotcoveringquot index is because SQL Server will quotbring along the non-key fieldsquot we used in the query, quotfor the ridequot. That way, SQL Server doesn39t need to use the table or the clustered index at all the database engine can simply use the covering index for the query. Covering indexes are popular in certain data warehousing and reporting database scenarios, though they do come at a cost of the database engine maintaining them. Note: Covering indexes have been around for a long time, so I haven39t yet covered the Columnstore index and the Query Store. So, I will add the covering index: If I re-execute the same query I ran a moment ago (the one that aggregated the sales amount for each product), the query sometimes seems to run about a second faster, and I get a different execution plan, one that uses an Index Seek instead of an Index Scan (using the date key on the covering index to retrieve sales for 2009). So, prior to the Columnstore Index, this could be one way to optimize this query in much older versions of SQL Server. It runs a little faster than the first one, and I get an execution plan with an Index Seek instead of an Index Scan. However, there are some issues: The two execution operators quotIndex Seekquot and quotHash Match (Aggregate)quot both essentially operate quotrow by rowquot. Imagine this in a table with hundreds of millions of rows. Related, think about the contents of a fact table: in this case, a single date key value andor a single product key value might be repeated across hundreds of thousands of rows (remember, the fact table also has keys for geography, promotion, salesman, etc.) So, when the quotIndex Seekquot and quotHash Matchquot work row by row, they are doing so over values that might be repeated across many other rows. This is normally where I39d segue to the SQL Server Columnstore index, which offers a scenario to improve the performance of this query in amazing ways. But before I do that, let39s go back in time. Let39s go back to the year 2010, when Microsoft introduced an add-in for Excel known as PowerPivot. Many people probably remember seeing demos of PowerPivot for Excel, where a user could read millions of rows from an outside data source into Excel. PowerPivot would compress the data, and provide an engine to create Pivot Tables and Pivot Charts that performed at amazing speeds against the compressed data. PowerPivot used an in-memory technology that Microsoft termed quotVertiPaqquot. This in-memory technology in PowerPivot would basically take duplicate business keyforeign key values and compress them down to a single vector. The in-memory technology would also scanaggregate these values in parallel, in blocks of several hundred at a time. The bottom line is that Microsoft baked a large amount of performance enhancements into the VertiPaq in-memory feature for us to use, right out of the proverbial box. Why am I taking this little stroll down memory lane Because in SQL Server 2012, Microsoft implemented one of the most important features in the history of their database engine: the Columnstore index. The index is really an index in name only: it is a way to take a SQL Server table and create a compressed, in-memory columnstore that compresses duplicate foreign key values down to single vector values. Microsoft also created a new buffer pool to read these compressed vector values in parallel, creating the potential for huge performance gains. So, I39m going to create a columnstore index on the table, and I39ll see how much better (and more efficiently) the query runs, versus the query that runs against the covering index. So, I39ll create a duplicate copy of FactOnlineSales (I39ll call it FactOnlineSalesDetailNCCS), and I39ll create a columnstore index on the duplicated table that way I won39t interfere with the original table and the covering index in any way. Next, I39ll create a columnstore index on the new table: Note several things: I39ve specified several foreign key columns, as well as the Sales Amount. Remember that a columnstore index is not like a traditional row-store index. There is no quotkeyquot. We are simply indicating which columns SQL Server should compress and place in an in-memory columnstore. To use the analogy of PowerPivot for Excel when we create a columnstore index, we39re telling SQL Server to essentially do the same thing that PowerPivot did when we imported 20 million rows into Excel using PowerPivot So, I39ll re-run the query, this time using the duplicated FactOnlineSalesDetailNCCS table that contains the columnstore index. This query runs instantly in less than a second. And I can also say that even if the table had hundreds of millions of rows, it would still run at the proverbial quotbat of an eyelashquot. We could look at the execution plan (and in a few moments, we will), but now it39s time to cover the Query Store feature. Imagine for a moment, that we ran both queries overnight: the query that used the regular FactOnlineSales table (with the covering index) and then the query that used the duplicated table with the Columnstore index. When we log in the following morning, we39d like to see the execution plan for both queries as they took place, as well as the execution statistics. In other words, we39d like to see the same statistics that we39d be able to see if we ran both queries interactively in SQL Management Studio, turned in TIME and IO Statistics, and viewed the execution plan right after executing the query. Well, that39s what the Query Store allows us to do we can turn on (enable) Query Store for a database, which will trigger SQL Server to store query execution and plan statistics so that we can view them later. So, I39m going to enable the Query Store on the Contoso database with the following command (and I39ll also clear out any caching): Then I39ll run the two queries (and quotpretendquot that I ran them hours ago): Now let39s pretend they ran hours ago. According to what I said, the Query Store will capture the execution statistics. So how do I view them Fortunately, that39s quite easy. If I expand the Contoso DW database, I39ll see a Query Store folder. The Query Store has tremendous functionality and I39ll try to cover much of it in subsequent blog posts. But for right now, I want to view execution statistics on the two queries, and specifically examine the execution operators for the columnstore index. So I39ll right-click on the Top Resource Consuming Queries and run that option. That gives me a chart like the one below, where I can see execution duration time (in milliseconds) for all queries that have been executed. In this instance, Query 1 was the query against the original table with the covering index, and Query 2 was against the table with the columnstore index. The numbers don39t lie the columnstore index outperformed the original tablecovering index by a factor of almost 7 to 1. I can change the metric to look at memory consumption instead. In this case, note that query 2 (the columnstore index query) used far more memory. This demonstrates clearly why the columnstore index represents quotin-memoryquot technology SQL Server loads the entire columnstore index in memory, and uses a completely different buffer pool with enhanced execution operators to process the index. OK, so we have some graphs to view execution statistics can we see the execution plan (and execution operators) associated with each execution Yes, we can If you click on the vertical bar for the query that used the columnstore index, you39ll see the execution plan below. The first thing we see is that SQL Server performed a columnstore index scan, and that represented nearly 100 of the cost of the query. You might be saying, quotWait a minute, the first query used a covering index and performed an index seek so how can a columnstore index scan be fasterquot That39s a legitimate question, and fortunately there39s an answer. Even when the first query performed an index seek, it still executed quotrow by rowquot. If I put the mouse over the columnstore index scan operator, I see a tooltip (like the one below), with one important setting: the Execution Mode is BATCH (as opposed to ROW . which is what we had with the first query using the covering index). That BATCH mode tells us that SQL Server is processing the compressed vectors (for any foreign key values that are duplicated, such as the product key and date key) in batches of almost 1,000, in parallel. So SQL Server is still able to process the columnstore index much more efficiently. Additionally, if I place the mouse over the Hash Match (Aggregate) task, I also see that SQL Server is aggregating the columnstore index using Batch mode (although the operator itself represents such a tiny percent of the cost of the query) Finally, you might be asking, quotOK, so SQL Server compresses the values in the data, treats the values as vectors, and read them in blocks of almost a thousand values in parallel but my query only wanted data for 2009. So is SQL Server scanning over the entire set of dataquot Again, a good question. The answer is, quotNot reallyquot. Fortunately for us, the new columnstore index buffer pool performs another function called quotsegment eliminationquot. Basically, SQL Server will examine the vector values for the date key column in the columnstore index, and eliminate segments that are outside the scope of the year 2009. I39ll stop here. In subsequent blog posts I39ll cover both the columnstore index and Query Store in more detail. Essentially, what we39ve seen here today is that the Columnstore index can significantly speed up queries that scanaggregate over large amounts of data, and the Query Store will capture query executions and allow us to examine execution and performance statistics later. In the end, we39d like to produce a result set that shows the following. Notice three things: The columns essentially pivot all of the possible Return Reasons, after showing the sales amount The result set contains subtotals by the week ending (Sunday) date across all clients (where the Client is NULL) The result set contains a grand total row (where the Client and Date are both NULL) First, before I get into the SQL end we could use the dynamic pivotmatrix capability in SSRS. We would simply need to combine the two result sets by one column and then we could feed the results to the SSRS matrix control, which will spread the return reasons across the columns axis of the report. However, not everyone uses SSRS (though most people should). But even then, sometimes developers need to consume result sets in something other than a reporting tool. So for this example, let39s assume we want to generate the result set for a web grid page and possibly the developer wants to quotstrip outquot the subtotal rows (where I have a ResultSetNum value of 2 and 3) and place them in a summary grid. So bottom line, we need to generate the output above directly from a stored procedure. And as an added twist next week there could be Return Reason X and Y and Z. So we don39t know how many return reasons there could be. We simple want the query to pivot on the possible distinct values for Return Reason. Here is where the T-SQL PIVOT has a restriction we need to provide it the possible values. Since we won39t know that until run-time, we need to generate the query string dynamically using the dynamic SQL pattern. The dynamic SQL pattern involves generating the syntax, piece by piece, storing it in a string, and then executing the string at the end. Dynamic SQL can be tricky, as we have to embed syntax inside a string. But in this case, it our only true option if we want to handle a variable number of return reasons. I39ve always found that the best way to create a dynamic SQL solution is by figuring out what the quotidealquot generated-query would be at the end (in this case, given the Return reasons we know about).and then reverse-engineering it by piecing it together one part at a time. And so, here is the SQL we need if we knew those Return Reasons (A through D) were static and would not change. The query does the following: Combines the data from SalesData with the data from ReturnData, where we quothard-wirequot the word Sales as an Action Type form the Sales Table, and then use the Return Reason from the Return Data into the same ActionType column. That will give us a clean ActionType column on which to pivot. We are combining the two SELECT statements into a common table expression (CTE), which is basically a derived table subquery that we subsequently use in the next statement (to PIVOT) A PIVOT statement against the CTE, that sums the dollars for the Action Type being in one of the possible Action Type values. Note that this isn39t the final result set. We are placing this into a CTE that reads from the first CTE. The reason for this is because we want to do multiple groupings at the end. The final SELECT statement, that reads from the PIVOTCTE, and combines it with a subsequent query against the same PIVOTCTE, but where we also implement two groupings in the GROUPING SETS feature in SQL 2008: GROUPING by the Week End Date (dbo. WeekEndingDate) GROUPING for all rows () So if we knew with certainty that we39d never have more return reason codes, then that would be the solution. However, we need to account for other reason codes. So we need to generate that entire query above as one big string where we construct the possible return reasons as one comma separated list. I39m going to show the entire T-SQL code to generate (and execute) the desired query. And then I39ll break it out into parts and explain each step. So first, here39s the entire code to dynamically generate what I39ve got above. There are basically five steps we need to cover. Schritt 1 . we know that somewhere in the mix, we need to generate a string for this in the query: SalesAmount, Reason A, Reason B, Reason C, Reason D0160016001600160 What we can do is built a temporary common table expression that combines the hard wired quotSales Amountquot column with the unique list of possible reason codes. Once we have that in a CTE, we can use the nice little trick of FOR XML PATH(3939) to collapse those rows into a single string, put a comma in front of each row that the query reads, and then use STUFF to replace the first instance of a comma with an empty space. This is a trick that you can find in hundreds of SQL blogs. So this first part builds a string called ActionString that we can use further down. Schritt 2 . we also know that we39ll want to SUM the generatedpivoted reason columns, along with the standard sales column. So we39ll need a separate string for that, which I39ll call SUMSTRING. I39ll simply use the original ActionString, and then REPLACE the outer brackets with SUM syntax, plus the original brackets. Step 3: Now the real work begins. Using that original query as a model, we want to generate the original query (starting with the UNION of the two tables), but replacing any references to pivoted columns with the strings we dynamically generated above. Also, while not absolutely required, I39ve also created a variable to simply any carriage returnline feed combinations that we want to embed into the generated query (for readability). So we39ll construct the entire query into a variable called SQLPivotQuery. Step 4 . We continue constructing the query again, concatenating the syntax we can quothard-wirequot with the ActionSelectString (that we generated dynamically to hold all the possible return reason values) Step 5 . Finally, we39ll generate the final part of the Pivot Query, that reads from the 2 nd common table expression (PIVOTCTE, from the model above) and generates the final SELECT to read from the PIVOTCTE and combine it with a 2 nd read against PIVOTCTE to implement the grouping sets. Finally, we can quotexecutequot the string using the SQL system stored proc spexecuteSQL So hopefully you can see that the process to following for this type of effort is Determine what the final query would be, based on your current set of data and values (i. e. built a query model) Write the necessary T-SQL code to generate that query model as a string. Arguably the most important part is determining the unique set of values on which you39ll PIVOT, and then collapsing them into one string using the STUFF function and the FOR XML PATH(3939) trick So whats on my mind today Well, at least 13 items Two summers ago, I wrote a draft BDR that focused (in part) on the role of education and the value of a good liberal arts background not just for the software industry but even for other industries as well. One of the themes of this particular BDR emphasized a pivotal and enlightened viewpoint from renowned software architect Allen Holub regarding liberal arts. Ill (faithfully) paraphrase his message: he highlighted the parallels between programming and studying history, by reminding everyone that history is reading and writing (and Ill add, identifying patterns), and software development is also reading and writing (and again, identifying patterns). And so I wrote an opinion piece that focused on this and other related topics. But until today, I never got around to either publishingposting it. Every so often Id think of revising it, and Id even sit down for a few minutes and make some adjustments to it. But then life in general would get in the way and Id never finish it. So what changed A few weeks ago, fellow CoDe Magazine columnist and industry leader Ted Neward wrote a piece in his regular column, Managed Coder , that caught my attention. The title of the article is On Liberal Arts. and I highly recommend that everyone read it. Ted discusses the value of a liberal arts background, the false dichotomy between a liberal arts background and success in software development, and the need to writecommunicate well. He talks about some of his own past encounters with HR personnel management regarding his educational background. He also emphasizes the need to accept and adapt to changes in our industry, as well as the hallmarks of a successful software professional (being reliable, planning ahead, and learning to get past initial conflict with other team members). So its a great read, as are Teds other CoDe articles and blog entries. It also got me back to thinking about my views on this (and other topics) as well, and finally motivated me to finish my own editorial. So, better late than never, here are my current Bakers Dozen of Reflections: I have a saying: Water freezes at 32 degrees . If youre in a trainingmentoring role, you might think youre doing everything in the world to help someone when in fact, theyre only feeling a temperature of 34 degrees and therefore things arent solidifying for them. Sometimes it takes just a little bit more effort or another ideachemical catalyst or a new perspective which means those with prior education can draw on different sources. Water freezes at 32 degrees . Some people can maintain high levels of concentration even with a room full of noisy people. Im not one of them occasionally I need some privacy to think through a critical issue. Some people describe this as you gotta learn to walk away from it. Stated another way, its a search for the rarefied air. This past week I spent hours in half-lit, quiet room with a whiteboard, until I fully understood a problem. It was only then that I could go talk with other developers about a solution. The message here isnt to preach how you should go about your business of solving problems but rather for everyone to know their strengths and what works, and use them to your advantage as much as possible. Some phrases are like fingernails on a chalkboard for me. Use it as a teaching moment is one. (Why is it like fingernails on a chalkboard Because if youre in a mentoring role, you should usually be in teaching moment mode anyway, however subtly). Heres another I cant really explain it in words, but I understand it. This might sound a bit cold, but if a person truly cant explain something in words, maybe they dont understand. Sure, a person can have a fuzzy sense of how something works I can bluff my way through describing how a digital camera works but the truth is that I dont really understand it all that well. There is a field of study known as epistemology (the study of knowledge). One of the fundamental bases of understanding whether its a camera or a design pattern - is the ability to establish context, to identify the chain of related events, the attributes of any components along the way, etc. Yes, understanding is sometimes very hard work, but diving into a topic and breaking it apart is worth the effort. Even those who eschew certification will acknowledge that the process of studying for certification tests will help to fill gaps in knowledge. A database manager is more likely to hire a database developer who can speak extemporaneously (and effortlessly) about transaction isolation levels and triggers, as opposed to someone who sort of knows about it but struggles to describe their usage. Theres another corollary here. Ted Neward recommends that developers take up public speaking, blogging, etc. I agree 100. The process of public speaking and blogging will practically force you to start thinking about topics and breaking down definitions that you might have otherwise taken for granted. A few years ago I thought I understood the T-SQL MERGE statement pretty well. But only after writing about it, speaking about, fielding questions from others who had perspectives that never occurred to me that my level of understanding increased exponentially. I know a story of a hiring manager who once interviewed an authordeveloper for a contract position. The hiring manager was contemptuous of publications in general, and barked at the applicant, So, if youre going to work here, would you rather be writing books or writing code Yes, Ill grant that in any industry there will be a few pure academics. But what the hiring manager missed was the opportunities for strengthening and sharpening skill sets. While cleaning out an old box of books, I came across a treasure from the 1980s: Programmers at Work. which contains interviews with a very young Bill Gates, Ray Ozzie, and other well-known names. Every interview and every insight is worth the price of the book. In my view, the most interesting interview was with Butler Lampson. who gave some powerful advice. To hell with computer literacy. Its absolutely ridiculous. Study mathematics. Learn to think. Lesen. Write. These things are of more enduring value. Learn how to prove theorems: A lot of evidence has accumulated over the centuries that suggests this skill is transferable to many other things. Butler speaks the truth . Ill add to that point learn how to play devils advocate against yourself. The more you can reality-check your own processes and work, the better off youll be. The great computer scientistauthor Allen Holub made the connection between software development and the liberal arts specifically, the subject of history. Here was his point: what is history Reading and writing. What is software development Among other things, reading and writing . I used to give my students T-SQL essay questions as practice tests. One student joked that I acted more like a law professor. Well, just like Coach Donny Haskins said in the movie Glory Road, my way is hard. I firmly believe in a strong intellectual foundation for any profession. Just like applications can benefit from frameworks, individuals and their thought processes can benefit from human frameworks as well. Thats the fundamental basis of scholarship. There is a story that back in the 1970s, IBM expanded their recruiting efforts in the major universities by focusing on the best and brightest of liberal arts graduates. Even then they recognized that the best readers and writers might someday become strong programmersystems analysts. (Feel free to use that story to any HR-type who insists that a candidate must have a computer science degree) And speaking of history: if for no other reason, its important to remember the history of product releases if Im doing work at a client site thats still using SQL Server 2008 or even (gasp) SQL Server 2005, I have to remember what features were implemented in the versions over time. Ever have a favorite doctor whom you liked because heshe explained things in plain English, gave you the straight truth, and earned your trust to operate on you Those are mad skills . and are the result of experience and HARD WORK that take years and even decades to cultivate. There are no guarantees of job success focus on the facts, take a few calculated risks when youre sure you can see your way to the finish line, let the chips fall where they may, and never lose sight of being just like that doctor who earned your trust. Even though some days I fall short, I try to treat my client and their data as a doctor would treat patients. Even though a doctor makes more money There are many clichs I detest but heres one I dont hate: There is no such thing as a bad question. As a former instructor, one thing that drew my ire was hearing someone criticize another person for asking a supposedly, stupid question. A question indicates a person acknowledges they have some gap in knowledge theyre looking to fill. Yes, some questions are better worded than others, and some questions require additional framing before they can be answered. But the journey from forming a question to an answer is likely to generate an active mental process in others. There are all GOOD things. Many good and fruitful discussions originate with a stupid question. I work across the board in SSIS, SSAS, SSRS, MDX, PPS, SharePoint, Power BI, DAX all the tools in the Microsoft BI stack. I still write some. NET code from time to time. But guess what I still spend so much time doing writing T-SQL code to profile data as part of the discovery process. All application developers should have good T-SQL chops. Ted Neward writes (correctly) about the need to adapt to technology changes. Ill add to that the need to adapt to clientemployer changes. Companies change business rules. Companies acquire other companies (or become the target of an acquisition). Companies make mistakes in communicating business requirements and specifications. Yes, we can sometimes play a role in helping to manage those changes and sometimes were the fly, not the windshield. These sometimes cause great pain for everyone, especially the I. T. people. This is why the term fact of life exists we have to deal with it. Just like no developer writes bug-free code every time, no I. T. person deals well with change every single time. One of the biggest struggles Ive had in my 28 years in this industry is showing patience and restraint when changes are flying from many different directions. Here is where my prior suggestion about searching for the rarified air can help. If you can manage to assimilate changes into your thought process, and without feeling overwhelmed, odds are youll be a significant asset. In the last 15 months Ive had to deal with a huge amount of professional change. Its been very difficult at times, but Ive resolved that change will be the norm and Ive tried to tweak my own habits as best I can to cope with frequent (and uncertain) change. Its hard, very hard. But as coach Jimmy Duggan said in the movie A League of Their Own: Of course its hard. If it wasnt hard, everyone would do it. The hard, is what makes it great . A powerful message. Theres been talk in the industry over the last few years about conduct at professional conferences (and conduct in the industry as a whole). Many respected writers have written very good editorials on the topic. Heres my input, for what its worth. Its a message to those individuals who have chosen to behave badly: Dude, it shouldnt be that hard to behave like an adult. A few years ago, CoDe Magazine Chief Editor Rod Paddock made some great points in an editorial about Codes of Conduct at conferences. Its definitely unfortunate to have to remind people of what they should expect out of themselves. But the problems go deeper. A few years ago I sat on a five-person panel (3 women, 2 men) at a community event on Women in Technology. The other male stated that men succeed in this industry because the Y chromosome gives men an advantage in areas of performance. The individual who made these remarks is a highly respected technology expert, and not some bozo making dongle remarks at a conference or sponsoring a programming contest where first prize is a date with a bikini model. Our world is becoming increasingly polarized (just watch the news for five minutes), sadly with emotion often winning over reason. Even in our industry, recently I heard someone in a position of responsibility bash software tool XYZ based on a ridiculous premise and then give false praise to a competing tool. So many opinions, so many arguments, but heres the key: before taking a stand, do your homework and get the facts . Sometimes both sides are partly rightor wrong. Theres only one way to determine: get the facts. As Robert Heinlein wrote, Facts are your single clue get the facts Of course, once you get the facts, the next step is to express them in a meaningful and even compelling way. Theres nothing wrong with using some emotion in an intellectual debate but it IS wrong to replace an intellectual debate with emotion and false agenda. A while back I faced resistance to SQL Server Analysis Services from someone who claimed the tool couldnt do feature XYZ. The specifics of XYZ dont matter here. I spent about two hours that evening working up a demo to cogently demonstrate the original claim was false. In that example, it worked. I cant swear it will always work, but to me thats the only way. Im old enough to remember life at a teen in the 1970s. Back then, when a person lost hisher job, (often) it was because the person just wasnt cutting the mustard. Fast-forward to today: a sad fact of life is that even talented people are now losing their jobs because of the changing economic conditions. Theres never a full-proof method for immunity, but now more than ever its critical to provide a high level of what I call the Three Vs (value, versatility, and velocity) for your employerclients. I might not always like working weekends or very late at night to do the proverbial work of two people but then I remember there are folks out there who would give anything to be working at 1 AM at night to feed their families and pay their bills. Always be yourselfyour BEST self. Some people need inspiration from time to time. Heres mine: the great sports movie, Glory Road. If youve never watched it, and even if youre not a sports fan I can almost guarantee youll be moved like never before. And Ill close with this. If you need some major motivation, Ill refer to a story from 2006. Jason McElwain, a high school student with autism, came off the bench to score twenty points in a high school basketball game in Rochester New York. Heres a great YouTube video. His mother said it all . This is the first moment Jason has ever succeeded and is proud of himself. I look at autism as the Berlin Wall. He cracked it. To anyone who wanted to attend my session at todays SQL Saturday event in DC I apologize that the session had to be cancelled. I hate to make excuses, but a combination of getting back late from Detroit (client trip), a car thats dead (blown head gasket), and some sudden health issues with my wife have made it impossible for me to attend. Back in August, I did the same session (ColumnStore Index) for PASS as a webinar. You can go to this link to access the video (itll be streamed, as all PASS videos are streamed) The link does require that you fill out your name and email address, but thats it. And then you can watch the video. Feel free to contact me if you have questions, at kgoffkevinsgoff. net November 15, 2013 Getting started with Windows Azure and creating SQL Databases in the cloud can be a bit daunting, especially if youve never tried out any of Microsofts cloud offerings. Fortunately, Ive created a webcast to help people get started. This is an absolute beginners guide to creating SQL Databases under Windows Azure. It assumes zero prior knowledge of Azure. You can go to the BDBI Webcasts of this website and check out my webcast (dated 11102013). Or you can just download the webcast videos right here: here is part 1 and here is part 2. You can also download the slide deck here. November 03, 2013 Topic this week: SQL Server Snapshot Isolation Levels, added in SQL Server 2005. To this day, there are still many SQL developers, many good SQL developers who either arent aware of this feature, or havent had time to look at it. Hopefully this information will help. Companion webcast will be uploaded in the next day look for it in the BDBI Webcasts section of this blog. October 26, 2013 Im going to start a weekly post of T-SQL tips, covering many different versions of SQL Server over the years Heres a challenge many developers face. Ill whittle it down to a very simple example, but one where the pattern applies to many situations. Suppose you have a stored procedure that receives a single vendor ID and updates the freight for all orders with that vendor id. create procedure dbo. UpdateVendorOrders update Purchasing. PurchaseOrderHeader set Freight Freight 1 where VendorID VendorID Now, suppose we need to run this for a set of vendor IDs. Today we might run it for three vendors, tomorrow for five vendors, the next day for 100 vendors. We want to pass in the vendor IDs. If youve worked with SQL Server, you can probably guess where Im going with this. The big question is how do we pass a variable number of Vendor IDs Or, stated more generally, how do we pass an array, or a table of keys, to a procedure Something along the lines of exec dbo. UpdateVendorOrders SomeListOfVendors Over the years, developers have come up with different methods: Going all the way back to SQL Server 2000, developers might create a comma-separated list of vendor keys, and pass the CSV list as a varchar to the procedure. The procedure would shred the CSV varchar variable into a table variable and then join the PurchaseOrderHeader table to that table variable (to update the Freight for just those vendors in the table). I wrote about this in CoDe Magazine back in early 2005 (code-magazinearticleprint. aspxquickid0503071ampprintmodetrue. Tip 3) In SQL Server 2005, you could actually create an XML string of the vendor IDs, pass the XML string to the procedure, and then use XQUERY to shred the XML as a table variable. I also wrote about this in CoDe Magazine back in 2007 (code-magazinearticleprint. aspxquickid0703041ampprintmodetrue. Tip 12)Also, some developers will populate a temp table ahead of time, and then reference the temp table inside the procedure. All of these certainly work, and developers have had to use these techniques before because for years there was NO WAY to directly pass a table to a SQL Server stored procedure. Until SQL Server 2008 when Microsoft implemented the table type. This FINALLY allowed developers to pass an actual table of rows to a stored procedure. Now, it does require a few steps. We cant just pass any old table to a procedure. It has to be a pre-defined type (a template). So lets suppose we always want to pass a set of integer keys to different procedures. One day it might be a list of vendor keys. Next day it might be a list of customer keys. So we can create a generic table type of keys, one that can be instantiated for customer keys, vendor keys, etc. CREATE TYPE IntKeysTT AS TABLE ( IntKey int NOT NULL ) So Ive created a Table Typecalled IntKeysTT . Its defined to have one column an IntKey. Nowsuppose I want to load it with Vendors who have a Credit Rating of 1..and then take that list of Vendor keys and pass it to a procedure: DECLARE VendorList IntKeysTT INSERT INTO VendorList SELECT BusinessEntityID from Purchasing. Vendor WHERE CreditRating 1 So, I now have a table type variable not just any table variable, but a table type variable (that I populated the same way I would populate a normal table variable). Its in server memory (unless it needs to spill to tempDB) and is therefore private to the connectionprocess. OK, can I pass it to the stored procedure now Well, not yet we need to modify the procedure to receive a table type. Heres the code: create procedure dbo. UpdateVendorOrdersFromTT IntKeysTT IntKeysTT READONLY update Purchasing. PurchaseOrderHeader set Freight Freight 1 FROM Purchasing. PurchaseOrderHeader JOIN IntKeysTT TempVendorList ON PurchaseOrderHeader. VendorID Te mpVendorList. IntKey Notice how the procedure receives the IntKeysTT table type as a Table Type (again, not just a regular table, but a table type). It also receives it as a READONLY parameter. You CANNOT modify the contents of this table type inside the procedure. Usually you wont want to you simply want to read from it. Well, now you can reference the table type as a parameter and then utilize it in the JOIN statement, as you would any other table variable. So dort haben Sie es. A bit of work to set up the table type, but in my view, definitely worth it. Additionally, if you pass values from. NET, youre in luck. You can pass an ADO. NET data table (with the same tablename property as the name of the Table Type) to the procedure. For. NET developers who have had to pass CSV lists, XML strings, etc. to a procedure in the past, this is a huge benefit. Finally I want to talk about another approach people have used over the years. SQL Server Cursors. At the risk of sounding dogmatic, I strongly advise against Cursors, unless there is just no other way. Cursors are expensive operations in the server, For instance, someone might use a cursor approach and implement the solution this way: DECLARE VendorID int DECLARE dbcursor CURSOR FASTFORWARD FOR SELECT BusinessEntityID from Purchasing. Vendor where CreditRating 1 FETCH NEXT FROM dbcursor INTO VendorID WHILE FETCHSTATUS 0 EXEC dbo. UpdateVendorOrders VendorID FETCH NEXT FROM dbcursor INTO VendorID The best thing Ill say about this is that it works. And yes, getting something to work is a milestone. But getting something to work and getting something to work acceptably are two different things. Even if this process only takes 5-10 seconds to run, in those 5-10 seconds the cursor utilizes SQL Server resources quite heavily. Thats not a good idea in a large production environment. Additionally, the more the of rows in the cursor to fetch and the more the number of executions of the procedure, the slower it will be. When I ran both processes (the cursor approach and then the table type approach) against a small sampling of vendors (5 vendors), the processing times where 260 ms and 60 ms, respectively. So the table type approach was roughly 4 times faster. But then when I ran the 2 scenarios against a much larger of vendors (84 vendors), the different was staggering 6701 ms versus 207 ms, respectively. So the table type approach was roughly 32 times faster. Again, the CURSOR approach is definitely the least attractive approach. Even in SQL Server 2005, it would have been better to create a CSV list or an XML string (providing the number of keys could be stored in a scalar variable). But now that there is a Table Type feature in SQL Server 2008, you can achieve the objective with a feature thats more closely modeled to the way developers are thinking specifically, how do we pass a table to a procedure Now we have an answer Hope you find this feature help. Feel free to post a comment.

No comments:

Post a Comment