r89288 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89287‎ | r89288 | r89289 >
Date:21:09, 1 June 2011
Author:ezachte
Status:deferred
Tags:
Comment:
added view and prepared statements
Modified paths:
  • /trunk/wikistats/analytics/create_and_use_db_analytics.txt (modified) (history)

Diff [purge]

Index: trunk/wikistats/analytics/create_and_use_db_analytics.txt
@@ -1,4 +1,5 @@
22 -- invoke this file with "mysql --user=root --password=[...] < create_and_use_db_analytics.txt"
 3+-- for test queries make sure to delete output files *test*.csv first (MySQL on purpose forbids overwrite)
34
45 -- tables implemented:
56 -- comscore
@@ -22,6 +23,7 @@
2324
2425 -- Create database and two tables from scratch
2526 DROP DATABASE IF EXISTS `analytics` ;
 27+
2628 CREATE DATABASE `analytics` ;
2729
2830 USE `analytics` ;
@@ -51,9 +53,9 @@
5254
5355 CREATE TABLE `comscore_regions` (
5456 `region_code` char (2),
55 - `target_language` char (10),
 57+ `report_language` char (10),
5658 `region_name` char (18),
57 - PRIMARY KEY (target_language,region_code)
 59+ PRIMARY KEY (report_language,region_code)
5860 ) ;
5961
6062 CREATE TABLE `wikistats` (
@@ -95,10 +97,10 @@
9698 ) ;
9799
98100 CREATE TABLE `language_names` (
99 - `target_language` char (15),
 101+ `report_language` char (15),
100102 `language_code` char (15),
101103 `language_name` char (50),
102 - PRIMARY KEY (target_language,language_code)
 104+ PRIMARY KEY (report_language,language_code)
103105 ) ;
104106
105107 -- SHOW TABLES ;
@@ -111,90 +113,117 @@
112114 -- Database Manipulation
113115 -- Obviously in real world this is a separate script
114116
115 -
116 -
117 -LOAD DATA INFILE 'c:/MySQL/analytics/analytics_in_comscore.csv'
 117+LOAD DATA LOCAL INFILE 'c:/MySQL/analytics/analytics_in_comscore.csv'
118118 INTO TABLE comscore
119119 FIELDS TERMINATED BY ','
120120 OPTIONALLY ENCLOSED BY '"'
121121 (yyyymm,country_code,region_code,property,project,reach,visitors) ;
122122
123 -LOAD DATA INFILE 'c:/MySQL/analytics/analytics_in_comscore_regions.csv'
 123+LOAD DATA LOCAL INFILE 'c:/MySQL/analytics/analytics_in_comscore_regions.csv'
124124 INTO TABLE comscore_regions
125125 FIELDS TERMINATED BY ','
126126 OPTIONALLY ENCLOSED BY '"'
127 - (target_language,region_code,region_name) ;
 127+ (report_language,region_code,region_name) ;
128128
129 -LOAD DATA INFILE 'c:/MySQL/analytics/analytics_in_wikistats.csv'
 129+-- show contents (debugging only)
 130+SELECT *
 131+ FROM comscore_regions ;
 132+
 133+LOAD DATA LOCAL INFILE 'analytics_in_wikistats.csv'
130134 INTO TABLE wikistats
131135 FIELDS TERMINATED BY ','
132136 OPTIONALLY ENCLOSED BY '"'
133137 (project,language_code,yyyymm,editors_all_time,editors_new,editors_ge_5,editors_ge_25,editors_ge_100,articles,articles_new_per_day,articles_over_bytes_500,articles_over_bytes_2000,edits_per_article,bytes_per_article,edits,size_in_bytes,size_in_words,links_internal,links_interwiki,links_image,links_external,redirects) ;
134138
135 -LOAD DATA INFILE 'c:/MySQL/analytics/analytics_in_page_views.csv'
 139+LOAD DATA LOCAL INFILE 'analytics_in_page_views.csv'
136140 INTO TABLE page_views
137141 FIELDS TERMINATED BY ','
138142 OPTIONALLY ENCLOSED BY '"'
139143 (project,language_code,yyyymm,views_non_mobile_raw,views_mobile_raw,views_non_mobile_normalized,views_mobile_normalized) ;
140144
141 -LOAD DATA INFILE 'c:/MySQL/analytics/analytics_in_language_names.csv'
 145+LOAD DATA LOCAL INFILE 'analytics_in_language_names.csv'
142146 INTO TABLE language_names
143147 FIELDS TERMINATED BY ','
144148 OPTIONALLY ENCLOSED BY '"'
145 - (target_language,language_code,language_name) ;
 149+ (report_language,language_code,language_name) ;
146150
147 -SELECT *
148 - FROM comscore
149 - ORDER BY yyyymm,country_code,region_code,property,project
150 - INTO OUTFILE 'c:/MySQL/analytics/analytics_out_comscore_full_table.csv'
151 - FIELDS TERMINATED BY ',' ;
152 -
153 -SELECT yyyymm, project, page_views.language_code, language_name, views_non_mobile_normalized, views_mobile_normalized
 151+-- on views procedures and functions see http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=28
 152+-- view makes join implicit and adds aggregated fields (and provides default order, can be overidden)
 153+CREATE VIEW page_views_v AS
 154+ SELECT yyyymm, project, page_views.language_code, language_name,
 155+ views_non_mobile_raw, views_mobile_raw ,
 156+ views_non_mobile_normalized, views_mobile_normalized,
 157+ views_non_mobile_raw+views_mobile_raw AS views_raw,
 158+ views_non_mobile_normalized+views_mobile_normalized AS views_normalized,
 159+ language_names.report_language
154160 FROM page_views LEFT JOIN language_names
155161 ON page_views.language_code = language_names.language_code
 162+ ORDER BY yyyymm,project,language_name ;
 163+
 164+PREPARE page_views_s1
 165+ FROM
 166+ "SELECT yyyymm,project,language_code,language_name,views_normalized
 167+ FROM page_views_v
156168 WHERE (project = 'wp') AND
157169 (yyyymm BETWEEN '2011-03' AND '2011-05') AND
158 - (page_views.language_code = 'nl') AND
159 - (language_names.target_language = 'en')
160 - ORDER BY project,language_name,yyyymm
 170+ (language_code = 'nl') AND
 171+ (report_language=?) -- parameter needed
161172 INTO OUTFILE 'c:/MySQL/analytics/analytics_out_page_views_test1.csv'
162 - FIELDS TERMINATED BY ',' ;
163 -
 173+ FIELDS TERMINATED BY ','" ;
164174
165 -SELECT yyyymm, project, page_views.language_code, language_name, views_non_mobile_normalized, views_mobile_normalized
166 - FROM page_views LEFT JOIN language_names
167 - ON page_views.language_code = language_names.language_code
168 - WHERE (project = 'wp') AND
169 - (yyyymm BETWEEN '2011-03' AND '2011-05') AND
170 - (page_views.language_code = 'nl') AND
171 - (language_names.target_language = 'de')
172 - ORDER BY project,language_name,yyyymm
 175+SET @report_language = 'en' ; -- used as parameter in prepared statements ; for tests databasee contains translations for 'en' and 'de'
 176+
 177+EXECUTE page_views_s1 USING @report_language ;
 178+
 179+SET @project = 'wp' ;
 180+SET @yyyymm_first = '2011-03' ;
 181+SET @yyyymm_last = '2011-05' ;
 182+SET @language_code = 'nl' ;
 183+SET @report_language = 'de' ;
 184+
 185+PREPARE page_views_s2
 186+ FROM
 187+ "SELECT yyyymm,project,language_code,language_name,views_normalized
 188+ FROM page_views_v
 189+ WHERE (project = ?) AND
 190+ (yyyymm BETWEEN ? AND ?) AND
 191+ (language_code = ?) AND
 192+ (report_language = ?)
173193 INTO OUTFILE 'c:/MySQL/analytics/analytics_out_page_views_test2.csv'
174 - FIELDS TERMINATED BY ',' ;
175 -
 194+ FIELDS TERMINATED BY ','" ;
176195
177 -SELECT yyyymm,region_name,reach
178 - FROM comscore LEFT JOIN comscore_regions
179 - ON comscore.region_code = comscore_regions.region_code AND comscore_regions.target_language = 'en'
180 - WHERE (region_name != '') AND (yyyymm BETWEEN '2011-03' AND '2011-05')
181 - ORDER BY yyyymm,region_name
182 - INTO OUTFILE 'c:/MySQL/analytics/analytics_out_comscore_reach_test1.csv'
183 - FIELDS TERMINATED BY ',' ;
 196+EXECUTE page_views_s2 USING @project, @yyyymm_first, @yyyymm_last, @language_code, @report_language ;
 197+
 198+
 199+/*
 200+-- in progress: tests with using procedure to make invocation more flexible than with prepared statement
 201+-- prep stats expects all parameters predefined
 202+
 203+-- could the following be done in MySQL: a variable where clause with variable number of variables ? (seems not exactly like in this example for other DMRS)
 204+-- http://www.sqlteam.com/article/implementing-a-dynamic-where-clause
 205+
 206+DROP PROCEDURE IF EXISTS page_views_p1 ;
 207+DELIMITER //
 208+CREATE PROCEDURE page_views_p1 (parm_project VARCHAR(30))
 209+ SELECT yyyymm,project,language_code,language_name,views_normalized
 210+ FROM page_views_v
 211+ WHERE (project = parm_project) ;
 212+ //
 213+DELIMITER ;
 214+
 215+-- CALL page_views_p1 ('wk') ;
 216+
 217+delimiter //
 218+CREATE PROCEDURE dynamic(IN tbl CHAR(64), IN col CHAR(64))
 219+BEGIN
 220+ SET @s = CONCAT('SELECT ',col,' FROM ',tbl) ;
 221+ PREPARE stmt FROM @s;
 222+ EXECUTE stmt;
 223+END
 224+//
 225+delimiter ;
 226+CALL dynamic ('comscore', 'yyyymm')
 227+*/
 228+
 229+
 230+

Status & tagging log