44
Data Analysis CPS352: Database Systems Simon Miner Gordon College Last Revised: 12/13/12

Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Data Analysis

CPS352: Database Systems

Simon Miner

Gordon College

Last Revised: 12/13/12

Page 2: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Agenda

• Check-in

• NoSQL Database “Presentations”

• Online Analytical Processing

• Data Mining

• Course Review

• Exam II

• Course Evaluations

Page 3: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Check-in

Page 4: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Online Analytical

Processing

Page 5: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Online Transaction Processing

(OLTP) • Transactional data – data associated with a single interaction of an end-

user with the organization maintaining the database

• Example

• Customer placing an order on an E-commerce website

• Account holder making a deposit at a bank

• Can be comprised of several rows/records of data

• Example: An order has records for the order itself, each line item, address, payment method, etc.

• Lots of data can accumulate quickly for numerous transactions

• Needed for its own sake (i.e. shipping orders, order history, monthly account statements, etc.

• Also useful for analysis as well…

• OLTP databases built and optimized for speed of transactions (both in the ACID and interaction contexts)

• i.e. Provisioned with smaller block sizes to facilitate more precise (and maybe quicker) read and write operations

Page 6: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Online Analytical Processing

(OLAP) • Decision support systems (DSS) to help organizations determine

longer term courses of action

• Example: Not many orders for a certain product, so adjust product offerings to better match customer desires

• Work with summaries and aggregations of raw transaction data

• OLAP user needs to have specific queries in mind

• Example: Give me a cross-tab of item type vs. color …

• Data mining – automated process to reveal patterns in data and system usage

• OLAP databases designed to handle large amounts of data

• i.e. Provisioned with larger block sizes to store and retrieve more data in read and write operations

Page 7: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Data Warehouse

• Unified repository for an organization’s historical OLAP data

• Supports trending, analysis, and decision making

• Gathered from numerous disparate sources via ETL processes

• Extract – get data from individual source(s) owned or managed by

various parties

• Transform – manipulate data so that it fits into the data warehousing

schema – i.e. de-duplication, summarization

• Load – store the transformed data in the data warehouse

• Data is loaded at regular intervals

• Slightly out of date, which is fine for analytical tasks the data

warehouse is used for

Page 8: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Data Warehouse Schema

• Dimension values are usually encoded using small integers

and mapped to full values via dimension tables

• Star schema

• Snowflake

schema

Page 9: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

A Data Warehouse in the

Clouds

“Amazon Redshift is a fast and powerful, fully

managed, petabyte-scale data warehouse service in the

cloud. Amazon Redshift offers you fast query

performance when analyzing virtually any size data set

using the same SQL-based tools and business

intelligence applications you use today. With a few

clicks in the AWS Management Console, you can

launch a Redshift cluster, starting with a few hundred

gigabytes of data and scaling to a petabyte or more, for

under $1,000 per terabyte per year.”

Page 10: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

OLAP Concepts

• Attribute types

• Dimension attribute – values to analyze on

• Explicit – color, size, price, customer type, etc.

• Derived – age (computed from DOB), ranges (years of experience)

• Measurement attribute – value summarized or aggregated over

various dimensions (sum, count, average, etc.)

• Cross-tab (pivot table) – tool allowing easy analysis of data

along various dimensions

• Available in tools like spreadsheets

• Basic SQL is not an effective tool to produce this kind of

structure (lots of dynamic “group by” queries needed)

Page 11: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Cross-Tab Example

Page 12: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

OLAP Operations

• Basic SQL

• Aggregate functions, like sum(), count(), average()

• Group by / having clause

• SQL-99 added support for operations to support

analytics processing

• Cube

• Rollup

• Rank / dense rank

Page 13: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Cube

• Structure to aggregate a single measurement

attribute across numerous dimensions

• Includes all possible combinations of dimension values

• Number of cube dimensions = number of dimensional

attributes

• Each dimension “row” includes a summary value for

the aggregate of all possible values of that dimension

• User slices cube for specific dimension values

Page 14: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Cube Example

Page 15: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Cube Slice Example

Slice showing sales for various combinations of item_name and color for size = medium

Slice from figure 18.3 in book

Page 16: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Cube Without Summaries

select item_name, color, size, sum(number)

from sales

group by item_name, color, size;

Page 17: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Another Cube Slice

Sales by item_name and color - for all sizes

select item_name, color, sum(number)

from sales

group by item_name, color;

Page 18: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Still Another Cube Slice

Sales by item_name and size - for all colors(not visible - all cells on bottom of cube, except front and right side)

select item_name, size, sum(number)

from sales

group by item_name, size;

Page 19: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Yet Another Cube Slice

Sales by color and size - for all item_names

select color, size, sum(number)

from sales

group by color, size;

Page 20: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

A Slice of a Cube Slice

Sales by item_name - for all colors and sizes

select item_name, sum(number)

from sales

group by item_name;

Page 21: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

How About a Slice with Your

Slice of Cube?

Sales by size - for all item_names and colors

select size, sum(number)

from sales

group by size, color;

Page 22: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Aggregates by the Slice

Sales by color - for all item_names and sizes

select color, sum(number)

from sales

group by color;

Page 23: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Slice Cubed

Total sales for all item_names, colors, and sizes

select sum(number)

from sales;

Page 24: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Slicing with SQL

select item_name, color, size, sum(number)

from sales

group by item_name, color, size;

select item_name, color, sum(number)

from sales

group by item_name, color;

select item_name, size, sum(number)

from sales

group by item_name, size;

select color, size, sum(number)

from sales

group by color, size;

select item_name, sum(number)

from sales

group by item_name;

select color, sum(number)

from sales

group by color;

select size, sum(number)

from sales

group by size;

select sum(number)

from sales;

• 2n SQL queries needed to generate all summary

representations for a cube

• For item_name, color, and size (3 dimensions), 23 = 8 queries

Page 25: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

SQL Cube Function

• cube ( dimension1, dimension2, … dimension)

• Used in the group by clause

• Produces all summary representations in the cube

• Examples

• select item_name, color, size, sum(number) from sales group by cube(item_name, color, size)

• select job, edlevel, sex, avg(salary) from employee group by cube(job, edlevel, sex) order by job, edlevel, sex;

• From sample DB2 database.

Page 26: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Rollup

• Summarize data based on the first listed dimension

• Similar to cube (which yields 2n groups) for n dimensions

• Includes all possible combinations of various dimensions and “all”

• Yields n+1 groups for n dimensions

• All the dimensions

• All dimensions except the last

• All the dimensions except the last and second to last

• rollup( dimension1, dimension2, … dimension) j --in group by clause

• Example

• select job, education, sex, avg(salary) from (select job, case when edlevel >= 18 then 'GRADUATE’ when edlevel >= 16 then 'COLLEGE’ else 'HIGH SCHOOL’ end as education, sex, salary from employee) as e group by rollup(job, education, sex) order by job, education, sex;

Page 27: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Rank

• Rank records over dimension attributes

• rank() over ( order by dimension sort_direction )

• Used in select clause

• Lower numbers mean higher rank (rank = 1 being highest)

• Example

• select firstname, lastname, salary, rank() over (order by edlevel desc) as edrank from employee order by salary desc;

Page 28: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Dense Rank

• Ranking function without skipping numbers

• dense_rank() over ( order by dimension sort_direction )

• Used in select clause

• Lower numbers mean higher rank (rank = 1 being highest)

• Example

• select firstname, lastname, salary, dense_rank() over (order by edlevel desc) as edrank from employee order by salary desc

Page 29: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Data Mining

Page 30: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Data Mining Concepts • Definition – systematic analysis of large data sets to uncover useful

patterns

• Often (partially) automated to find patterns that human user might not anticipate

• Different from OLAP in which a human user enters a specific query for summarized data

• Look for conditions or rules in data that allow general conclusions to be drawn

• Prediction – forecast likely scenarios or outcomes based on patterns in past data

• Example: credit scores, derived from a person’s past borrowing, spending, and (re)payment, are used to determine eligibility for loans

• Uses tools like decision trees

• Association

• Example: Find books that are often bought by “similar” customers. If a new such customer buys one such book, suggest the others too.

• Uses processes like collaborative filtering

Page 31: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Decision Tree Example

degree

income income income income

bachelors masters doctoratenone

bad average good

bad average good excellent

50K 100K25K =25K

=50K50K

25K 75K

25 to 75K50 to 100K

Page 32: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Collaborative Filtering

• “Customers who viewed/carted/bought this product also liked these…”

• Recommendation engines

• Users rate items

• Explicitly via reviews, ratings, shares, etc.

• Implicitly via views, clicks, purchases, etc.

• System stores these ratings and builds neighborhoods based on similarities in rating behavior

• System recommends similar items based on current item or rating history

• Not limited to e-commerce

• Companies track store visits and purchases to offer custom coupons and determine best product placement

• Loyalty programs give discounts on products

Page 33: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Course Review

Page 34: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Modeling in the Logical Layer

• Relational model

• Entities connected by relationships

• Keys and nulls

• Schema Diagrams

• Relational algebra

• Selection

• Projection

• Joins – Cartesian/natural/theta

• Set operators – union/difference/intersection

• Rename

• Outer join

• Aggregate functions

• Entity relationship model

• Entities and entity sets

• Relationships and relationship sets

• Attributes – atomic/composite/derived/multi-valued

• Mapping cardinalities

• One-to-one

• One-to-many

• Many-to-many

• Total participation constraint

• Weak entities

• Generalized and specialized entities

Page 35: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

SQL

• DML

• Select

• Joins

• Group by / having

• Order by

• Subqueries

• Recursive queries

• Insert

• Update

• Delete

• Commit / rollback

• DDL

• Create/alter/drop table

• Create view

• Create index

• Integrity constraints

• Primary key/unique

• Foreign key (referential)

• Domain constraints

• Check clause

• Triggers

• Security

• User accounts

• Grant/revoke statements

Page 36: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Database Normalization

• Functional dependencies (FD)

• Closure

• Canonical cover

• Super/candidate/primary key

• Multi-valued dependencies (MVD)

• Decomposition

• Lossless join

• Dependency preserving

• Database design goals

• Avoid redundancies

• Ensure lossless join

• Ensure dependency preserving decompositions

• Normal forms

• 1NF – atomic

• 2NF – no partial key dependencies

• 3NF – no transitive dependencies

• BCNF – key, whole key, and nothing but the key

• 4NF – BCNF + non-redundant MVDs

Page 37: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Database Application

Development

• Evolution of database clients (thick -> thin -> web)

• Database access from applications –

• Dynamic (JDBC-style)

• Static/embedded (SQLJ)

• Object relational mapping

• Application architecture

• Two- vs. three-tier models

• MVC – Model (+business) / View / Controller layers

Page 38: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Database Physical Layer

• Minimize disk accesses

• Storage system setup

• Disk vs. memory buffer

• Record organization

• Fixed vs. variable length

• Sequential vs. multi-table clustering

• Indexing

• Ordered vs. hashed

• Clustered vs. non-clustered

• Dense vs. sparse

• B+ tree indexes (and searches)

• Query optimization

• Selection – full table, index based on type (exact vs. range)

• Join strategies

• Nested loop

• Nested block

• Buffering entire relation

• Merge join

• Ordering joins + equivalence rules

• Push selections inward

• Push projections outward

• Estimating join size with statistics

Page 39: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Concurrency

• ACID

• Transactions

• Transaction states

• Schedules

• Serializability

• Precedence graphs

• Recovery – cascading rollback

• Crash recovery

• Transaction log

• Approaches

• Incremental log with deferred update

• Incremental log with immediate update

• Shadow paging

• Locking

• Granularity of locks

• Shared vs. exclusive locks

• Deadlock

• Two-phase locking protocol

• Grow and shrink phases

• Other concurrency approaches

• Timestamps

• Validation – optimistic concurrency

• Multiversion schemes

• Inserts, deletes and phantom rows

• Relaxing consistency

Page 40: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Database Architectures

• Centralized systems

• Parallelism

• Speed-up vs. scale-up – batch vs. transaction scale-up

• Distributed systems

• Fragmentation (horizontal vs. vertical)

• Replication

• Data transparency

• Two-phase commit protocol

• Concurrency issues (locking, timestamps)

Page 41: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

NoSQL

• Why NoSQL?

• Common characteristics

• Aggregate-oriented

databases

• Schema-less databases

• Scaling vs. consistency

• Sharding and replication

• Update and read consistency

• Map-reduce pattern

• Data models

• Key-value databases

• Document databases

• Column-family databases

• Graph databases

• Schema migrations

• Polyglot persistence

• When (not) to use NoSQL

• NewSQL (Google Spanner)

Page 42: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Data Analysis

• OLTP vs. OLAP

• Data warehouses

• OLAP concepts

• Dimension vs. measurement attributes

• Cube

• Rollup

• Rank and dense rank

• Data mining

• Prediction (decision tree)

• Association (collaborative filtering)

Page 43: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Exam II

Page 44: Database Application Development€¦ · • Data mining – automated process to reveal patterns in data and system usage • OLAP databases designed to handle large amounts of data

Course Evaluations