查看: 17463|回复: 8

[笔记] 大家一起来测试,benchmark起来(MySQL下的TPC-C,TPC-H,TPC-W)

[复制链接]
论坛徽章:
0
跳转到指定楼层
1#
发表于 2010-7-27 22:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
前段时间在MySQL下做了些类基准测试,帖出小结。
其中遇到许多小问题,不知道有遗漏步骤没,也还有些问题没解决,希望大家讨论讨论,一起测试起来吧,嘿嘿~


环境:
ubuntu9.10
Server version: 5.1.37-1ubuntu5.4 (Ubuntu)



一、TPC-C(参考:http://samurai-mysql.blogspot.com/2009/03/settingup-dbt-2.html        Settingup DBT-2,需要挂代理,附1)

1)安装MySQL及相关组件
  略

2)下载DBT-2
  http://osdldbt.sourceforge.net/

3)安装所需要的perl包(可参考:http://blog.sina.com.cn/s/blog_48c95a190100h7yq.html)
  shell> sudo cpan Statistics:: Descriptive
  shell> sudo cpan Test::Parser
  shell> sudo cpan Test::Reporter

  (参考资料:http://bbs.chinaunix.net/archiver/?tid-881542.html,
             http://blog.csdn.net/yhb72/archive/2005/11/27/537667.aspx
             http://bbs3.chinaunix.net/thread-1076095-2-1.html)

4)编译DBT-2
  进入相应的DBT-2目录
  cd /home/liova/download/dbt2-0.40/
  执行:
  shell> sudo apt-get install build-essentials
  shell> ./configure --with-mysql=/usr/local/mysql [options] && make
  shell> sudo make install

5)生成所需要的数据库记录
  shell> datagen -w 10 -d /var/tmp/dbt2 --mysql
  -w 指定了数据仓库的个数
  -d 指定了生成的数据所在的目录

7)ubuntu下需要生成一个临时连接,因为mysqld.sock不在tmp目录下(以后每次重启系统都需要先执行这句,如果嫌麻烦,可以修改MySQL的配置文件,把sock文件生成到tmp目录下)
  ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

6)把生成的记录载入MySQL,在DBT-2目录下执行
  shell> sudo scripts/mysql/build_db.sh -w 3 -d dbt2 -f /var/tmp/dbt2 -s /tmp/mysql.sock -h localhost -u user -p password -e INNODB
  -d 数据库名
  -f 之前生成的数据库记录所在的目录
  -u MySQL的用户名
  -p 相应用户的密码

7)设置环境变量
  shell> export USE_PGPOOL=0
  shell> export LD_LIBRARY_PATH=/usr/local/mysql/lib
  
8)运行DBT-2
  shell> sudo scripts/run_workload.sh -c 16 -d 300 -n -w 10 -s 10 -u user -x password
  -c 并发的线程数
  -d 执行的时间(秒)
  -n 无思考时间,作者说如果想看数据库性能,请不要漏掉这个选项
  -w 数据仓库个数
  -s 每个新线程开始前的延迟时间(微秒)
  -u MySQL用户名
  -x MySQL相应用户的密码
  -l MySQL TCP/IP端口号
  -H MySQL主机名

输出的结果在/home/liova/download/dbt2-0.40/scripts/output/        对应的实验目录下的
如:/home/liova/download/dbt2-0.40/scripts/output/17/driver/results.out

[ 本帖最后由 liova 于 2010-7-27 22:34 编辑 ]
论坛徽章:
0
2#
 楼主| 发表于 2010-7-27 22:23 | 只看该作者
二、TPC-H(参考:http://my2iu.blogspot.com/2009/0 ... eries-on-mysql.html        Running TPC-H Queries on MySQL,需要挂代理,附2;http://www.pilhokim.com/index.php?title=Project/EFIM/TPC-H        Project/EFIM/TPC-H  By Pilho Kim)

1)下载TPC-H的代码,用来生成所需要的数据
  http://www.tpc.org/tpch/spec/tpch_2_8_0.zip 或者
  http://www.tpc.org/tpch/default.asp        右边栏
  两版本略有不同

2)创建MySQL用户、数据库、及授权
  mysql -u root -p
  mysql> CREATE USER 'tpch'@'%' IDENTIFIED BY 'tpch';
  mysql> CREATE DATABASE tpch;
  mysql> GRANT ALL ON tpch.* to 'tpch'@'%';
  mysql> USE tpch;

3)运行tpch目录下的dss.ddl文件,创建tpch数据库中的表
  mysql> \. tpch/gen/dss.ddl

4)然后在tpch文件目录下,把makefile复制并改名成makefile,接着修改makefile文件
  shell> cp makefile.suite makefile
  shell> vim makefile
  makefile中相应项后面填写:
  CC      = gcc  # Current values for DATABASE are: INFORMIX, DB2, TDAT (Teradata)  #                                  SQLSERVER, SYBASE  # Current values for MACHINE are:  ATT, DOS, HP, IBM, ICL, MVS,   #                                  SGI, SUN, U2200, VMS, LINUX, WIN32   # Current values for WORKLOAD are:  TPCH  DATABASE= SQLSERVER  MACHINE = LINUX  WORKLOAD = TPCH       

5)修改tpch.h文件
  修改其中的SQLSERVER段为:
  #ifdef  SQLSERVER
  #define GEN_QUERY_PLAN        "EXPLAIN;"
  #define START_TRAN                "START TRANSACTION;\n"
  #define END_TRAN                "COMMIT;\n"
  #define SET_OUTPUT                ""
  #define SET_ROWCOUNT                "limit %d;\n"
  #define SET_DBASE                "use %s;\n"
  #endif

6)执行makefile
  shell> make

7)生成需要用的数据
  shell> ./dbgen -s 1
  -s 数据规模因子,1为1G的数据量

8)修改tpch目录下的dss.ri文件
  1.删除 "CONNECT TO TPCD;"
  2.删除所有的 "TPCH."(注意有个点)
  3.删除所有的"COMMIT WORK;"(注意分号也要删除)

9)登录MySQL,并载入之前生成的数据进MySQL
  shell> mysql -u tpch -p
  mysql> use tpch;
  mysql> LOAD DATA LOCAL INFILE 'customer.tbl' INTO TABLE CUSTOMER FIELDS TERMINATED BY '|';
  mysql> LOAD DATA LOCAL INFILE 'orders.tbl' INTO TABLE ORDERS FIELDS TERMINATED BY '|';
  mysql> LOAD DATA LOCAL INFILE 'lineitem.tbl' INTO TABLE LINEITEM FIELDS TERMINATED BY '|';
  mysql> LOAD DATA LOCAL INFILE 'nation.tbl' INTO TABLE NATION FIELDS TERMINATED BY '|';
  mysql> LOAD DATA LOCAL INFILE 'partsupp.tbl' INTO TABLE PARTSUPP FIELDS TERMINATED BY '|';
  mysql> LOAD DATA LOCAL INFILE 'part.tbl' INTO TABLE PART FIELDS TERMINATED BY '|';
  mysql> LOAD DATA LOCAL INFILE 'region.tbl' INTO TABLE REGION FIELDS TERMINATED BY '|';
  mysql> LOAD DATA LOCAL INFILE 'supplier.tbl' INTO TABLE SUPPLIER FIELDS TERMINATED BY '|';

10)执行tpch目录下的dss.ri
  mysql> \. dss.ri

11)由于查询中使用的是小写表名,而使用dss.ddl生成的表名是大写的,所以转换表名成小写
  mysql> alter table NATION rename nation;
  mysql> alter table SUPPLIER rename supplier;
  mysql> alter table REGION rename region;
  mysql> alter table PARTSUPP rename partsupp;
  mysql> alter table PART rename part;
  mysql> alter table ORDERS rename orders;
  mysql> alter table LINEITEM rename lineitem;
  mysql> alter table CUSTOMER rename customer;

12)执行(最后这步有点错误,暂时没找到解决的办法,哪位老大解决了教我呀呀呀,thx~~)
  cp dists.dss queries
  cd queries
  ../qgen -c tpch -s 1 1

12)我所用的替代方法
  在tpch目录的queries子目录下,新建文件:Q1.sql,Q2.sql,…,Q21,sql,Q22.sql
  在每个文件中添加相应的查询(http://www.pilhokim.com/index.ph ... C-H_default_queries,TPC-H的默认查询,经过MySQL 5.1.23测试通过,附3)

13)登录MySQL,手动执行TPC-H的默认查询
  shell> mysql -u tpch -p
  mysql> use tpch;
  mysql> source /home/liova/download/tpch/queries/Q1.sql;
  mysql> source /home/liova/download/tpch/queries/Q2.sql;
  …
  …
  mysql> source /home/liova/download/tpch/queries/Q22.sql;

[ 本帖最后由 liova 于 2010-7-27 22:25 编辑 ]

使用道具 举报

回复
论坛徽章:
0
3#
 楼主| 发表于 2010-7-27 22:23 | 只看该作者
三、TPC-W (相关文档:Installing and Getting TPC-W to run on Virtuoso-12.pdf         By Ashish Gupta以及网站http://mitglied.multimania.de/jankiefer/tpcw/index.html, TPC-W Java Implementation)

1)下载 TPC-W Java版 http://mitglied.multimania.de/jankiefer/tpcw/index.html
  比较难下载,很经常下一半丢失连接,多试几次,下完全的文件tpcw-java-dist-1.0.zip大小为1.13M。

2)下载并安装Tomcat(可参考http://deepfuture.广告.com/blog/591885)
  1.安装jdk6
    shell>sudo apt-get install sun-java6-jdk(用新得立软件包安装)
  2.下载tomcat6
    下载地址是:http://tomcat.apache.org/
    本次安装的是:apache-tomcat-6.0.18.tar.gz
  3.安装
    将压缩包拷贝到你自己想放的目录下,这里是/home/liova/download/tpcw/
    shell>sudo cp apache-tomcat-6.0.18.tar.gz /home/liova/download/tpcw/
    解压文件
    shell>cd /home/liova/download/tpcw/
    shell>sudo tar zxvf apache-tomcat-6.0.18.tar.gz


    解压完成后,会在 /home/liova/download/tpcw/ 下生成文件夹 apache-tomcat-6.0.18

    在/usr/local 下建立软链接,名字是tomcat6
    shell>cd /usr/local
    shell>sudo ln -s /home/liova/download/tpcw/apache-tomcat-6.0.18 tomcat6

    测试tomcat是否能够正常启动
    shell>sudo /usr/local/tomcat6/bin/startup.sh        (这里图简单没有配置tomcat开机启动,所以以后每次重启都要先执行这条命令开启tomcat)
    打开firefox,输入http://localhost:8080/,如果正常访问,则表示成功。

3)下载并安装Apache Ant来编译Java代码(可参考http://wangxc.广告.com/blog/665521)
  1.下载地址 Ant:
    地址:http://ant.apache.org/
  2.解压ANT包
    tar zxvf apache-ant-1.8.1-bin.tar.gz
  3.将解压的Ant文件移动到/opt/ant目录下
    mv apache-ant-1.8.1 /opt/ant
  4.修改配置文件中的环境ANT_HOME
    sudo vim /etc/profile
    在文件末尾添加如下,在umask前加上#
   
    #umask 022   
    export JAVA_HOME=/usr/lib/jvm/java-6-sun   
    export MVN_HOME=/opt/maven   
    export ANT_HOME=/opt/ant   
    export PATH=$ANT_HOME/bin:$MVN_HOME/bin:$PATH:

4)安装MySQL
  略.

5)下载JDBC MySQL驱动
  http://dev.mysql.com/downloads/connector/j/3.0.html
  解压后复制mysql-connector-java-3.0.15-ga-bin.jar到此路径下(不存在则自己创建):/usr/local/tomcat/webapps/tpcw/WEB-INF/lib

6)下载servlet.jar
  下完复制到此路径下(不存在则自己创建):/usr/local/jakarta-tomcat-6/server/lib/servlet.jar

7)设置参数
  1.根据自己的路径在/etc/profile末尾添加:   
   export CLASSPATH=/usr/local/jakarta-tomcat-6/server/lib/servlet.jar:/usr/lib/jvm/:/home/liova/download/tpcw/tpc-w/:/usr/lib/jvm/java-6-sun-1.6.0.20/lib/dt.jar:/usr/lib/jvm/java-6-sun-1.6.0.20/lib/tools.jar:$CLASSPATH
  2.解压刚开始下载的tpcw-java-dist-1.0.zip文件,我这里解压到路径/home/liova/download/tpcw/tpc-w/下,修改其中的main.properties和tpcw.properties
   
    下面是我的main.properties(最后的dbName确实要用std,但之后我们在数据库里建的数据库名是用tpcw):

##############################################################################
# main.properties for build.xml.
# Copyright 2003 by Jan Kiefer.
#
# This file is distributed "as is". It comes with no warranty and the
# author takes no responsibility for the consequences of its use.
#
# Usage, distribution and modification is allowed to everyone, as long
# as reference to the author(s) is given and this license note is included.
###############################################################################
<!-- Path to servlet.jar, change this ... -->
cpServ=/usr/local/jakarta-tomcat-6/server/lib/servlet.jar

#<!-- Path to the JDBC driver for your DBMS, change this ... -->
cpJDBC=/usr/local/tomcat6/webapps/tpcw/WEB-INF/lib/mysql-connector-java-3.0.17-ga-bin.jar

#<!-- Directory where tpcw.war will be put with task 'inst' -->
webappDir=/usr/local/tomcat6/webapps/tpcw

#<!-- Path to the Perl interpreter. -->
perlPath=/usr/bin/perl

#<!-- Directory where the Images will be put with task genimg. -->
imagesDir=${webappDir}/Images

#<!-- Filter file for SQL queries, change this if needed -->
sqlFilter=sql-mysql.properties

dbName=std
#dbName=tpcw


    以及我的tpcw.properties:

##############################################################################
# tpcw.properties for build.xml.
# Copyright 2003 by Jan Kiefer.
#
# This file is distributed "as is". It comes with no warranty and the
# author takes no responsibility for the consequences of its use.
#
# Usage, distribution and modification is allowed to everyone, as long
# as reference to the author(s) is given and this license note is included.
##############################################################################
# set the JDBC parameters
jdbc.driver=com.mysql.jdbc.Driver
#jdbc.driver=com.mckoi.JDBCDriver
jdbc.path=jdbc:mysql://localhost:8080/tpcw?user=tpcw&password=tpcw
jdbc.connPoolMax=100

#sql.bigCharType=varchar(500)
sql.bigCharType=tinyblob

# set the values you want for tpcw
num.item=1000
num.eb=10

# use the right session string for your servlet container
#sessionIdString=$sessionid$
sessionIdString=jsessionid=

standardUrl=http://localhost:8080
#servletUrlPath=/servlet
servletUrlPath=/
tpcwUrlPath=/tpcw
   

8)在MySQL中传见数据库tpcw,并给数据库tpcw的权限给用户tpcw
  create database tpcw;
  GRANT ALL PRIVILEGES ON tpcw.* TO tpcw@'%' IDENTIFIED BY "tpcw" WITH GRANT OPTION;

9)修改函数名。
  /home/liova/download/tpcw/tpc-w/src/rbe/util/Debug.java中
  public class Debug {  public static void assert(boolean assertCond, String message)

  把assert改成你自己想要的名字,我是改成assert1。
  接着以下三个文件中所有出现的Debug.assert中的assert都改成你自己修改函数名,我这里改完是Debug.assert1
  /home/liova/download/tpcw/tpc-w/src/rbe/EB.java
  /home/liova/download/tpcw/tpc-w/src/rbe/util/CharSetStrPattern.java
  /home/liova/download/tpcw/tpc-w/src/rbe/util/Histogram.java

10)cd /home/liova/download/tpcw/tpc-w/
  1.执行命令
    ant dist        (这条命令用来编译servlets以及rbe java文件)
    ant inst        (这命令执行后,将把tpcw.war包移动到tomcat的tpcw目录下)  
  2.创建数据库中的表及记录,执行
    ant gendb        (这命令将调用TPCW_populate.class)
  3.创建图像
    ant genimg        (这命令将用make编译图像生成工具,用perl去运行图像生成脚本,复制静态图像到tomcat主目录)

11)修改/usr/local/tomcat6/conf/下的server.xml,在<host></host>标签中间添加如下代码:
   <Context path="/tpcw" docBase="/tpcw/tpcw.war" debug="0" reloadable="false" crossContext="true" privileged="false">
   <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_tpcw_log." suffix=".txt" verbosity="0" timestamp="true"/>
   </Context>



做到这里就安装好了,可以运行了。
可以访问http://localhost:8080/tpcw/TPCW_home_interaction页面测试是否安装成功。



12)运行TPC-W
   1.cd /home/liova/download/tpcw/tpc-w/dist/
   2.sudo java rbe.RBE -EB rbe.EBTPCW1Factory 30 -OUT run1.m -RU 100 -MI 1000 -RD 100 -WWW http://localhost:8080/tpcw/ -CUST 10000 -ITEM 10000

   命令的解释可参考/home/liova/download/tpcw/tpc-w/docs/use.html以及/home/liova/download/tpcw/tpc-w/dist/doc/readme-rbe.txt

   输出的结果在/home/liova/download/tpcw/tpc-w/dist/下的run1.m文件中(名字自己在参数的指定)




ps:
如果运行中出现EB error, unable to open url……, 可以看见url中有UNAME和PASSWD字段,这代表用户名和密码。一般出错的原因是数据库tpcw中的customer表中没有相对应用户的记录(所有用户信息都在customer表中),可能是由于创建数据库记录过程中一些记录的用户名和密码被截断了,我因为图简单没去找代码中哪出错(哪为老大要是解决了希望能分享啊分享啊啊啊,thx~~),我直接修改了数据库中的相应记录。比如:
tpcw中customer表中,我把某条记录修改为:
c_uname                c_passwdOGALININRE        ogalininre        ->        OGALINININRE        ogalinininre

[ 本帖最后由 liova 于 2010-7-27 22:27 编辑 ]

使用道具 举报

回复
论坛徽章:
0
4#
 楼主| 发表于 2010-7-27 22:24 | 只看该作者
附1:
Settingup DBT-2

DBT-2 is a TPC-C like OLTP benchmark, and very popular amongst many MySQL users. It is used by MySQL QA team to test the stability and performance before release. However, steps to setup DBT-2 is a little bit messy, and its README files include some dummy information. So I introduce you these steps below:

1. Download it!

You can download the source code from here: http://osdldbt.sourceforge.net/

2. Required packages

The following perl packages are required to build DBT-2. Unfortunately, configure script doesn't complain even if they are missing. Install them using, e.g. CPAN.

shell> sudo cpan Statistics:escriptive
shell> sudo cpan Test::Parser
shell> sudo cpan Test::Reporter

If you want to make a graph from the output, you have to install gnuplot in advance. e.g. Ubuntu users can install it like below:

shell> sudo apt-get install gnuplot

3. Compile it

You can build the software in very standard way, configure and make. For MySQL users, --with-mysql option is mandatory. If you want to specify install path, then use --prefix option.

shell> ./configure --with-mysql=/usr/local/mysql [options] && make
shell> sudo make install

Ubuntu users may need to install build-essentials package in advance.

shell> sudo apt-get install build-essentials

4. Generate and load data

shell> datagen -w 10 -d /var/tmp/dbt2 --mysql

datagen is a command to generate data under the given directory. -w option specifies the number of warehouse, more is larger data. If you want to test CPU bound test, use small value.

shell> scripts/mysql/build_db.sh \
-w 3 -d dbt2 -f /var/tmp/dbt2 -s /tmp/mysql.sock \
-h localhost -u user -p password -e INNODB

Then, you can load the data generated by datagen program using build_db.sh, located under scripts/mysql directory under the source code directory. The following indicates the size of data with 10 warehouses for example.

mysql> select table_name,sum(data_length) from tables where table_schema='dbt2' group by table_name with rollup;
+------------+------------------+
| table_name | sum(data_length) |
+------------+------------------+
| customer   |        200081408 |
| district   |            16384 |
| history    |         25755648 |
| item       |         11026432 |
| new_order  |          4702208 |
| orders     |         22626304 |
| order_line |        314572800 |
| stock      |        381648896 |
| warehouse  |            16384 |
| NULL       |        960446464 |
+------------+------------------+
10 rows in set (0.54 sec)

5. Run benchmark

shell> export USE_PGPOOL=0
shell> export LD_LIBRARY_PATH=/usr/local/mysql/lib
shell> scripts/run_workload.sh -c 16 -d 600 -n -w 10 -s 10 -u user -x password

The benchmark is done using run_workload.sh script. This script accepts the following option.

-c ... thread concurrency
-d ... duration
-n ... no thinking time. do not miss this option if you want to see the database performance
-w ... number of warehouse
-s ... delay before starting each new thread in millisecond
- u ... MySQL user name
-x ... MySQL password
-l ... MySQL TCP/IP port number
-H ... MySQL server hostname

Then, the benchmark runs during the period specified by -d option, and generates data under ./output directory. You may see the error like following. This error indicates that no data is written in the ./output directory. It is not a Test::Parser:bt2.pm problem at all!

Can't use an undefined value as an ARRAY reference at /usr/local/share/perl/5.10.0/Test/Parser/Dbt2.pm line 521.

One reason why this error appears is that libmysqlclient.so wasn't loaded correctly. Do not forget specifying the right PATH to the library before the benchmark.

shell> export LD_LIBRARY_PATH=/usr/local/mysql/lib


Enjoy!!

[ 本帖最后由 liova 于 2010-7-27 22:28 编辑 ]

使用道具 举报

回复
论坛徽章:
0
5#
 楼主| 发表于 2010-7-27 22:24 | 只看该作者
附2:

Running TPC-H Queries on MySQL


Getting the TPC-H queries to work on MySQL isn't too hard, but it isn't always clear what to do. I took the instructions from here and converted them to work with MySQL.

Once TPC-H is started, you can create a database and tpch account:

mysql -u root -p
mysql> CREATE USER 'tpch'@'%' IDENTIFIED BY 'password';
mysql> CREATE DATABASE tpch;
mysql> GRANT ALL ON tpch.* to 'tpch'@'%';
mysql> USE tpch;
mysql> \. tpch/gen/dss.ddl

Then, in the gen directories, you can modify the query generator to generate queries that are as close as possible to what you need:

cp makefile.suite makefile
#Modify makefile to use
# CC = gcc, DATABASE=SQLSERVER, MACHINE=LINUX, WORKLOAD=TPCH
#In tpcd.h, SQLSERVER section:
# change #define SET_DBASE "use %s;\n"
# change #define SET_ROWCOUNT "limit %d;\n\n"
# change #define START_TRAN "BEGIN WORK;"
# change #define END_TRAN "COMMIT WORK;"
make

Then you can start generating database data at the right scale factor

./dbgen -s 1

And also modify the constraints/indices file so that it's compatible with mysql:

# Modify dss.ri
# use a search and replace in order to remove "CONNECT TO TPCD", remove references to "TPCD." and remove the lines "COMMIT WORK;"

Then you can load all the data into the databases with

mysql -u tpch -p
mysql> use tpch;
mysql> LOAD DATA LOCAL INFILE 'customer.tbl' INTO TABLE CUSTOMER FIELDS TERMINATED BY '|';
# Same with orders, lineitem, nation, partsupp, part, region, supplier
mysql> \. dss.ri

You then need to change the case of the table names because the queries use lower-case table names I think whereas the dss.ddl uses upper-case names for the tables:

mysql> alter table NATION rename nation;
# Ditto for supplier, region, partsupp, part, orders, lineitem, customer

Finally, you can test some of the queries

cp dists.dss queries
cd queries
../qgen -c tpch -s 1 1

I think the queries still need to be modified a bit to be compatible. I think queries with a limit may need the semi-colon moved around, the precision indicator during date arithmetic in query 1 may need to be removed, and the method for naming columns in query 13 might need changing.

[ 本帖最后由 liova 于 2010-7-27 22:28 编辑 ]

使用道具 举报

回复
论坛徽章:
0
6#
 楼主| 发表于 2010-7-27 22:24 | 只看该作者
# Q1
select
        l_returnflag,
        l_linestatus,
        sum(l_quantity) as sum_qty,
        sum(l_extendedprice) as sum_base_price,
        sum(l_extendedprice * (1 - l_discount)) as sum_disc_price,
        sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) as sum_charge,
        avg(l_quantity) as avg_qty,
        avg(l_extendedprice) as avg_price,
        avg(l_discount) as avg_disc,
        count(*) as count_order
from
        lineitem
where
        l_shipdate <= date '1998-12-01' - interval '90' day
group by
        l_returnflag,
        l_linestatus
order by
        l_returnflag,
        l_linestatus;

# Q2
select
        s_acctbal,
        s_name,
        n_name,
        p_partkey,
        p_mfgr,
        s_address,
        s_phone,
        s_comment
from
        part,
        supplier,
        partsupp,
        nation,
        region
where
        p_partkey = ps_partkey
        and s_suppkey = ps_suppkey
        and p_size = 15
        and p_type like '%BRASS'
        and s_nationkey = n_nationkey
        and n_regionkey = r_regionkey
        and r_name = 'EUROPE'
        and ps_supplycost = (
                select
                        min(ps_supplycost)
                from
                        partsupp,
                        supplier,
                        nation,
                        region
                where
                        p_partkey = ps_partkey
                        and s_suppkey = ps_suppkey
                        and s_nationkey = n_nationkey
                        and n_regionkey = r_regionkey
                        and r_name = 'EUROPE'
        )
order by
        s_acctbal desc,
        n_name,
        s_name,
        p_partkey
limit 100;

#Q3
select
        l_orderkey,
        sum(l_extendedprice * (1 - l_discount)) as revenue,
        o_orderdate,
        o_shippriority
from
        customer,
        orders,
        lineitem
where
        c_mktsegment = 'BUILDING'
        and c_custkey = o_custkey
        and l_orderkey = o_orderkey
        and o_orderdate < date '1995-03-15'
        and l_shipdate > date '1995-03-15'
group by
        l_orderkey,
        o_orderdate,
        o_shippriority
order by
        revenue desc,
        o_orderdate
limit 10;

#Q4
select
        o_orderpriority,
        count(*) as order_count
from
        orders
where
        o_orderdate >= date '1993-07-01'
        and o_orderdate < date '1993-07-01' + interval '3' month
        and exists (
                select
                        *
                from
                        lineitem
                where
                        l_orderkey = o_orderkey
                        and l_commitdate < l_receiptdate
        )
group by
        o_orderpriority
order by
        o_orderpriority;

#Q5
select
        n_name,
        sum(l_extendedprice * (1 - l_discount)) as revenue
from
        customer,
        orders,
        lineitem,
        supplier,
        nation,
        region
where
        c_custkey = o_custkey
        and l_orderkey = o_orderkey
        and l_suppkey = s_suppkey
        and c_nationkey = s_nationkey
        and s_nationkey = n_nationkey
        and n_regionkey = r_regionkey
        and r_name = 'ASIA'
        and o_orderdate >= date '1994-01-01'
        and o_orderdate < date '1994-01-01' + interval '1' year
group by
        n_name
order by
        revenue desc;

#Q6
select
        sum(l_extendedprice * l_discount) as revenue
from
        lineitem
where
        l_shipdate >= date '1994-01-01'
        and l_shipdate < date '1994-01-01' + interval '1' year
        and l_discount between .06 - 0.01 and .06 + 0.01
        and l_quantity < 24;

#Q7
select
        supp_nation,
        cust_nation,
        l_year,
        sum(volume) as revenue
from
        (
                select
                        n1.n_name as supp_nation,
                        n2.n_name as cust_nation,
                        extract(year from l_shipdate) as l_year,
                        l_extendedprice * (1 - l_discount) as volume
                from
                        supplier,
                        lineitem,
                        orders,
                        customer,
                        nation n1,
                        nation n2
                where
                        s_suppkey = l_suppkey
                        and o_orderkey = l_orderkey
                        and c_custkey = o_custkey
                        and s_nationkey = n1.n_nationkey
                        and c_nationkey = n2.n_nationkey
                        and (
                                (n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY')
                                or (n1.n_name = 'GERMANY' and n2.n_name = 'FRANCE')
                        )
                        and l_shipdate between date '1995-01-01' and date '1996-12-31'
        ) as shipping
group by
        supp_nation,
        cust_nation,
        l_year
order by
        supp_nation,
        cust_nation,
        l_year;

#Q8
select
        o_year,
        sum(case
                when nation = 'BRAZIL' then volume
                else 0
        end) / sum(volume) as mkt_share
from
        (
                select
                        extract(year from o_orderdate) as o_year,
                        l_extendedprice * (1 - l_discount) as volume,
                        n2.n_name as nation
                from
                        part,
                        supplier,
                        lineitem,
                        orders,
                        customer,
                        nation n1,
                        nation n2,
                        region
                where
                        p_partkey = l_partkey
                        and s_suppkey = l_suppkey
                        and l_orderkey = o_orderkey
                        and o_custkey = c_custkey
                        and c_nationkey = n1.n_nationkey
                        and n1.n_regionkey = r_regionkey
                        and r_name = 'AMERICA'
                        and s_nationkey = n2.n_nationkey
                        and o_orderdate between date '1995-01-01' and date '1996-12-31'
                        and p_type = 'ECONOMY ANODIZED STEEL'
        ) as all_nations
group by
        o_year
order by
        o_year;

#Q9
select
        nation,
        o_year,
        sum(amount) as sum_profit
from
        (
                select
                        n_name as nation,
                        extract(year from o_orderdate) as o_year,
                        l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
                from
                        part,
                        supplier,
                        lineitem,
                        partsupp,
                        orders,
                        nation
                where
                        s_suppkey = l_suppkey
                        and ps_suppkey = l_suppkey
                        and ps_partkey = l_partkey
                        and p_partkey = l_partkey
                        and o_orderkey = l_orderkey
                        and s_nationkey = n_nationkey
                        and p_name like '%green%'
        ) as profit
group by
        nation,
        o_year
order by
        nation,
        o_year desc;


#Q10
select
        c_custkey,
        c_name,
        sum(l_extendedprice * (1 - l_discount)) as revenue,
        c_acctbal,
        n_name,
        c_address,
        c_phone,
        c_comment
from
        customer,
        orders,
        lineitem,
        nation
where
        c_custkey = o_custkey
        and l_orderkey = o_orderkey
        and o_orderdate >= date '1993-10-01'
        and o_orderdate < date '1993-10-01' + interval '3' month
        and l_returnflag = 'R'
        and c_nationkey = n_nationkey
group by
        c_custkey,
        c_name,
        c_acctbal,
        c_phone,
        n_name,
        c_address,
        c_comment
order by
        revenue desc
limit 20;


#Q11
select
        ps_partkey,
        sum(ps_supplycost * ps_availqty) as value
from
        partsupp,
        supplier,
        nation
where
        ps_suppkey = s_suppkey
        and s_nationkey = n_nationkey
        and n_name = 'GERMANY'
group by
        ps_partkey having
                sum(ps_supplycost * ps_availqty) > (
                        select
                                sum(ps_supplycost * ps_availqty) * 0.0001000000
                        from
                                partsupp,
                                supplier,
                                nation
                        where
                                ps_suppkey = s_suppkey
                                and s_nationkey = n_nationkey
                                and n_name = 'GERMANY'
                )
order by
        value desc;


#Q12
select
        l_shipmode,
        sum(case
                when o_orderpriority = '1-URGENT'
                        or o_orderpriority = '2-HIGH'
                        then 1
                else 0
        end) as high_line_count,
        sum(case
                when o_orderpriority <> '1-URGENT'
                        and o_orderpriority <> '2-HIGH'
                        then 1
                else 0
        end) as low_line_count
from
        orders,
        lineitem
where
        o_orderkey = l_orderkey
        and l_shipmode in ('MAIL', 'SHIP')
        and l_commitdate < l_receiptdate
        and l_shipdate < l_commitdate
        and l_receiptdate >= date '1994-01-01'
        and l_receiptdate < date '1994-01-01' + interval '1' year
group by
        l_shipmode
order by
        l_shipmode;

#Q13
# modified
select
        c_count,
        count(*) as custdist
from
        (
                select
                        c_custkey,
                        count(o_orderkey) as c_count
                from
                        customer left outer join orders on
                                c_custkey = o_custkey
                                and o_comment not like '%special%requests%'
                group by
                        c_custkey
        ) as c_orders
group by
        c_count
order by
        custdist desc,
        c_count desc;

#Q14
select
        100.00 * sum(case
                when p_type like 'PROMO%'
                        then l_extendedprice * (1 - l_discount)
                else 0
        end) / sum(l_extendedprice * (1 - l_discount)) as promo_revenue
from
        lineitem,
        part
where
        l_partkey = p_partkey
        and l_shipdate >= date '1995-09-01'
        and l_shipdate < date '1995-09-01' + interval '1' month;


#Q15
create view revenue0 (supplier_no, total_revenue) as
        select
                l_suppkey,
                sum(l_extendedprice * (1 - l_discount))
        from
                lineitem
        where
                l_shipdate >= date '1996-01-01'
                and l_shipdate < date '1996-01-01' + interval '3' month
        group by
                l_suppkey;


select
        s_suppkey,
        s_name,
        s_address,
        s_phone,
        total_revenue
from
        supplier,
        revenue0
where
        s_suppkey = supplier_no
        and total_revenue = (
                select
                        max(total_revenue)
                from
                        revenue0
        )
order by
        s_suppkey;

drop view revenue0;


#Q16
select
        p_brand,
        p_type,
        p_size,
        count(distinct ps_suppkey) as supplier_cnt
from
        partsupp,
        part
where
        p_partkey = ps_partkey
        and p_brand <> 'Brand#45'
        and p_type not like 'MEDIUM POLISHED%'
        and p_size in (49, 14, 23, 45, 19, 3, 36, 9)
        and ps_suppkey not in (
                select
                        s_suppkey
                from
                        supplier
                where
                        s_comment like '%Customer%Complaints%'
        )
group by
        p_brand,
        p_type,
        p_size
order by
        supplier_cnt desc,
        p_brand,
        p_type,
        p_size;


#Q17
select
        sum(l_extendedprice) / 7.0 as avg_yearly
from
        lineitem,
        part
where
        p_partkey = l_partkey
        and p_brand = 'Brand#23'
        and p_container = 'MED BOX'
        and l_quantity < (
                select
                        0.2 * avg(l_quantity)
                from
                        lineitem
                where
                        l_partkey = p_partkey
        );


#Q18
select
        c_name,
        c_custkey,
        o_orderkey,
        o_orderdate,
        o_totalprice,
        sum(l_quantity)
from
        customer,
        orders,
        lineitem
where
        o_orderkey in (
                select
                        l_orderkey
                from
                        lineitem
                group by
                        l_orderkey having
                                sum(l_quantity) > 300
        )
        and c_custkey = o_custkey
        and o_orderkey = l_orderkey
group by
        c_name,
        c_custkey,
        o_orderkey,
        o_orderdate,
        o_totalprice
order by
        o_totalprice desc,
        o_orderdate
limit 100;


#Q19
select
        sum(l_extendedprice* (1 - l_discount)) as revenue
from
        lineitem,
        part
where
        (
                p_partkey = l_partkey
                and p_brand = 'Brand#12'
                and p_container in ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG')
                and l_quantity >= 1 and l_quantity <= 1 + 10
                and p_size between 1 and 5
                and l_shipmode in ('AIR', 'AIR REG')
                and l_shipinstruct = 'DELIVER IN PERSON'
        )
        or
        (
                p_partkey = l_partkey
                and p_brand = 'Brand#23'
                and p_container in ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK')
                and l_quantity >= 10 and l_quantity <= 10 + 10
                and p_size between 1 and 10
                and l_shipmode in ('AIR', 'AIR REG')
                and l_shipinstruct = 'DELIVER IN PERSON'
        )
        or
        (
                p_partkey = l_partkey
                and p_brand = 'Brand#34'
                and p_container in ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG')
                and l_quantity >= 20 and l_quantity <= 20 + 10
                and p_size between 1 and 15
                and l_shipmode in ('AIR', 'AIR REG')
                and l_shipinstruct = 'DELIVER IN PERSON'
        );


#Q20
select
        s_name,
        s_address
from
        supplier,
        nation
where
        s_suppkey in (
                select
                        ps_suppkey
                from
                        partsupp
                where
                        ps_partkey in (
                                select
                                        p_partkey
                                from
                                        part
                                where
                                        p_name like 'forest%'
                        )
                        and ps_availqty > (
                                select
                                        0.5 * sum(l_quantity)
                                from
                                        lineitem
                                where
                                        l_partkey = ps_partkey
                                        and l_suppkey = ps_suppkey
                                        and l_shipdate >= date '1994-01-01'
                                        and l_shipdate < date '1994-01-01' + interval '1' year
                        )
        )
        and s_nationkey = n_nationkey
        and n_name = 'CANADA'
order by
        s_name;


#Q21
select
        s_name,
        count(*) as numwait
from
        supplier,
        lineitem l1,
        orders,
        nation
where
        s_suppkey = l1.l_suppkey
        and o_orderkey = l1.l_orderkey
        and o_orderstatus = 'F'
        and l1.l_receiptdate > l1.l_commitdate
        and exists (
                select
                        *
                from
                        lineitem l2
                where
                        l2.l_orderkey = l1.l_orderkey
                        and l2.l_suppkey <> l1.l_suppkey
        )
        and not exists (
                select
                        *
                from
                        lineitem l3
                where
                        l3.l_orderkey = l1.l_orderkey
                        and l3.l_suppkey <> l1.l_suppkey
                        and l3.l_receiptdate > l3.l_commitdate
        )
        and s_nationkey = n_nationkey
        and n_name = 'SAUDI ARABIA'
group by
        s_name
order by
        numwait desc,
        s_name
limit 100;


#Q22
select
        cntrycode,
        count(*) as numcust,
        sum(c_acctbal) as totacctbal
from
        (
                select
                        substring(c_phone from 1 for 2) as cntrycode,
                        c_acctbal
                from
                        customer
                where
                        substring(c_phone from 1 for 2) in
                                ('13', '31', '23', '29', '30', '18', '17')
                        and c_acctbal > (
                                select
                                        avg(c_acctbal)
                                from
                                        customer
                                where
                                        c_acctbal > 0.00
                                        and substring(c_phone from 1 for 2) in
                                                ('13', '31', '23', '29', '30', '18', '17')
                        )
                        and not exists (
                                select
                                        *
                                from
                                        orders
                                where
                                        o_custkey = c_custkey
                        )
        ) as custsale
group by
        cntrycode
order by
        cntrycode;

[ 本帖最后由 liova 于 2010-7-27 22:29 编辑 ]

使用道具 举报

回复
论坛徽章:
0
7#
发表于 2010-9-2 10:30 | 只看该作者

回复 #3 liova 的帖子

你好,您能够把tpcw-java-dist-1.0.zip文件上传一份吗?你提供的网址怎么老是打不开呢?非常感谢!

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
8#
发表于 2010-12-20 10:26 | 只看该作者
nice job

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
9#
发表于 2010-12-20 12:16 | 只看该作者

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表