金旭亮
===============================================
EntityFramework(EF)是微软平台主流的数据存取技术。为了给学生介绍这一技术,我制作了三讲Entity Framework 5.0教学PPT,包括相应源码及示例数据库。
教学内容主要参考JuliaLerman所著之EF三部曲:
《ProgrammingEntity Framework》SECOND EDITION
《ProgrammingEntity Framework:Code First》
《ProgrammingEntity Framework:DbContext》
读后感:第一本是EF 4.0框架的全面介绍,非常详尽深入的砖头书,要啃下来很不容易。
第二本和第三本都是介绍EF 4.X的,与当前EF 5.0基本一致,有一点小变化,不影响大局。
个人感觉:《DbContext》一书写得比较浅显易懂,推荐阅读。
本教学PPT的目的是引导初学者掌握Entity Framework 5.0的基本使用方法,让其能迅速把握要点并用于实际开发中。要求初学者己掌握C#编程语言,会使用SQL Server完成创建表等基本操作。
主要内容:
第一讲: Entity Framework概述
第二讲: 数据的查询、增删改查操作、EF数据更新原理
第三讲:并发访问
如果是初学者,对EF所知甚少,建议先看我的教学PPT,并且把里面的示例全部看懂,之后自己创建一个数据库,把我设计的实例的功能自己动手实现一遍。
接着访问其官方网站:http://www.asp.net/entity-framework,在其“GetStarted”部分(http://msdn.microsoft.com/zh-cn/data/ee712907)查询其详尽信息。
EF在微软的其它技术中得到了较多的应用,想了解这方面的示例,推荐访问http://www.asp.net/mvc,完成Intro to ASP.NET MVC4(http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4)和MVC MusicStore 两个官方教程。
在实际开发中,EF多用于创建数据存取层,我推荐中大型项目使用Database-First模式,小型项目采用Code-First,有关在多层系统中使用EF构建数据存取层的相关内容,本PPT未涉及,会在后面的系统架构设计课程中介绍。
本教学PPT在北京理工大学《面向对象技术与方法》课程中使用。由于本人才疏学浅,教学PPT和示例中如有错误,请在本文跟帖指出更正。
访问CSDN下载频道 点击下载三讲《EntityFramework》学习资源及实例。
注:示例采用Visual studio 2012,EF版本5.0,使用数据库SQL Server 2008 Express R2。
arg_time in varchar2,
out_result out varchar2) is
/*
运营分析-实时数据分析
功能:查询
创建时间:2012-3-*
创建人:binliu
更新历史:
*/
sql_str varchar2(2000);
temp_max varchar2(200);
temp_min varchar2(200);
temp_partitionArr varchar2(2000);
temp_partition varchar2(200);
temp_index number(20);
temp_tableCount number(20);
temp_sum number default(0);
temp_date varchar2(2000);
temp_result varchar2(2000);
temp_count number;
begin
--创建临时表
select count(*)
into temp_tableCount
from user_tables
where table_name = 'CALL_TEMP_REALTIMEDATA';
if (temp_tableCount = 0) then
begin
sql_str := '
create global temporary table call_temp_realtimedata
(
callid number(19),
starttime date
)
on commit preserve rows;
';
execute immediate sql_str;
end;
end if;
temp_date := arg_voiceDate || ' ' || arg_time;
--计算表分区区间
if arg_voiceDate is not null then
select min(t.startid), max(t.endid)
into temp_min, temp_max
from call_dayinfo t
where t.voicedate between
to_date(arg_voiceDate || ' 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and
to_date(arg_voiceDate || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss');
end if;
select transformpartition(temp_min, temp_max)
into temp_partitionArr
from dual;
dbms_output.put_line('startid:' || temp_min);
dbms_output.put_line('endid:' || temp_max);
dbms_output.put_line(temp_partitionArr);
--把数据放入临时表中
temp_index := instr(temp_partitionArr, '/');
while temp_index <> 0 loop
temp_partition := substr(temp_partitionArr, 1, temp_index - 1);
--判断分区时间
sql_str := 'select count(t1.id)
from call_callrecord partition(' || temp_partition ||
') t1
where t1.starttime between to_date(''' || arg_voiceDate || ' ' ||
arg_time || ''',''yyyy-mm-dd hh24:mi:ss'')-1/24/4
and to_date(''' || arg_voiceDate || ' ' ||
arg_time || ''',''yyyy-mm-dd hh24:mi:ss'')+1/8
';
execute immediate sql_str
into temp_count;
if temp_count <> 0 then
sql_str := 'select t1.callid , t2.starttime from call_modelcall partition( ' ||
temp_partition ||
' ) t1 inner join call_callrecord partition( ' ||
temp_partition ||
' ) t2 on t2.id = t1.callid where modelid in
(select id from voiceinsightbase.base_specialmodel t3 where t3.modeltype = 4)
and t2.starttime between to_date(''' ||
arg_voiceDate || ' ' || arg_time ||
''',''yyyy-mm-dd hh24:mi:ss'')-1/24/4
and to_date(''' || arg_voiceDate || ' ' ||
arg_time || ''',''yyyy-mm-dd hh24:mi:ss'')+1/8
';
sql_str := 'insert into call_temp_realtimedata (' || sql_str || ')';
dbms_output.put_line('sql_str is:' || sql_str);
execute immediate sql_str;
commit;
end if;
temp_partitionArr := substr(temp_partitionArr, temp_index + 1);
temp_index := instr(temp_partitionArr, '/');
end loop;
--从临时表中查数据
sql_str := 'select count(t.callid) from call_temp_realtimedata t where t.starttime between to_date(''' ||
arg_voiceDate || ' ' || arg_time ||
''',''yyyy-mm-dd hh24:mi:ss'')-1/24/4
and to_date(''' || arg_voiceDate || ' ' ||
arg_time || ''',''yyyy-mm-dd hh24:mi:ss'')
';
dbms_output.put_line('sql_str = ' || sql_str);
execute immediate sql_str
into temp_sum;
temp_result := temp_sum;
temp_index := 1;
while temp_index <= 12 loop
sql_str := 'select count(t.callid) from call_temp_realtimedata t where t.starttime between to_date(''' ||
arg_voiceDate || ' ' || arg_time ||
''',''yyyy-mm-dd hh24:mi:ss'')+(1/24/4)*' ||
(temp_index - 1) || '
and to_date(''' || arg_voiceDate || ' ' ||
arg_time || ''',''yyyy-mm-dd hh24:mi:ss'')+(1/24/4)*' ||
temp_index;
dbms_output.put_line('sql_str2 --' || sql_str);
execute immediate sql_str
into temp_sum;
temp_result := temp_result || '-' || temp_sum;
temp_index := temp_index + 1;
end loop;
dbms_output.put_line(temp_result);
out_result := temp_result;
end CALL_RealTimeCallReason2;
观察Oracle数据库性能,Oracle自带的awr 功能为我们提供了一个近乎完美的解决方案,通过awr特性我们可以随时从数据库提取awr报告。不过awrrpt.sql脚本执行时需要我们提供一些交互信息,因此可以将其整合到shell脚本中来实现自动产生指定时段的awr报告并发送给相关人员。本文对Linux/Unix shell 自动发送AWR report的功能进行了完善和补充。
1、shell脚本
robin@SZDB:~/dba_scripts/custom/awr> more autoawr_by_time.sh #!/bin/bash # --------------------------------------------------------------------------+ # Generate AWR report and send mail automatically | # Filename: autoawr_by_time.sh | # Desc: | # The script use to generate awr report by time period. | # Three parameter for it. | # para1: <ORACLE_SID> mandatory parameter | # para2: [begin time] optional parameter | # para3: [end time ] optional parameter | # Deploy it by crontab as requirement | # Usage: | # ./autoawr_by_time.sh <instance_name> [begin time] [end time] | # Example: | # ./autoawr_by_time.sh TESTDB | # --default,time period is from last midnight to today midnight | # ./autoawr_by_time.sh TESTDB 2013031009 | # --time period is from 2013031009 to now | # ./autoawr_by_time.sh TESTDB 2013031009 2013031012 | # --time period by speicifed | # Author : Robinson | # Blog : http://blog.csdn.net/robinson_0612 | # --------------------------------------------------------------------------+ # # ------------------------------- # Set environment here # ------------------------------ if [ -f ~/.bash_profile ]; then . ~/.bash_profile fi # ------------------------------------------------------------ # Check the parameter, if no specify,then use default value # ------------------------------------------------------------ if [ -z "${1}" ] ;then echo "Usage: " echo " `basename $0` <ORACLE_SID> [begin_date] [end_date]" fi if [ -z "${3}" ] && [ -z "${2}" ];then begin_date=`date -d yesterday +%Y%m%d`'00' end_date=`date +%Y%m%d`'00' elif [ -z "${3}" ]; then begin_date=${2} end_date=`date +%Y%m%d%H` else begin_date=${2} end_date=${3} fi ORACLE_SID=${1} export ORACLE_SID begin_date end_date export MACHINE=`hostname` export MAIL_DIR=/users/robin/dba_scripts/sendEmail-v1.56 export MAIL_LIST='Robinson.chen@2GoTrade.com' export AWR_CMD=/users/robin/dba_scripts/custom/awr export AWR_DIR=/users/robin/dba_scripts/custom/awr/report/${ORACLE_SID} export MAIL_FM='oracle@szdb.com' RETENTION=31 echo $ORACLE_SID echo $begin_date echo $end_date # -------------------------------------------------------------------- # Check the directory for store awr report,if not exist, create it # -------------------------------------------------------------------- if [ ! -d "${AWR_DIR}" ]; then mkdir -p ${AWR_DIR} fi # ---------------------------------------------- # check if the database is running, if not exit # ---------------------------------------------- db_stat=`ps -ef | grep pmon_$ORACLE_SID | grep -v grep| cut -f3 -d_` if [ -z "$db_stat" ]; then #date >/tmp/db_${ORACLE_SID}_stauts.log echo " $ORACLE_SID is not available on ${MACHINE} !!!" # >>/tmp/db_${ORACLE_SID}_stauts.log MAIL_SUB=" $ORACLE_SID is not available on ${MACHINE} !!!" MAIL_BODY=" $ORACLE_SID is not available on ${MACHINE} at `date` when try to generate AWR." $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_BODY exit 1 fi; # --------------------------------------------- # Generate the awr report # --------------------------------------------- sqlplus -S "/ as sysdba" @${AWR_CMD}/autoawr_by_time.sql $begin_date $end_date status=$? if [ $status != 0 ];then echo " $ORACLE_SID is not available on ${MACHINE} !!!" # >>/tmp/db_${ORACLE_SID}_stauts.log MAIL_SUB=" Occurred error while generate AWR for ${ORACLE_SID} !!!" MAIL_BODY=" Some exceptions encountered during generate AWR report for $ORACLE_SID on `hostname`." $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_BODY exit fi # ------------------------------------------------ # Send email with AWR report # ------------------------------------------------ filename=`ls ${AWR_DIR}/${ORACLE_SID}_awrrpt_?_${begin_date}_${end_date}*` if [ -e "${filename}" ];then MAIL_SUB="AWR report from ${ORACLE_SID} on `hostname`." MAIL_BODY="This is an AWR report from ${ORACLE_SID} on `hostname`.Time period: $begin_date,$end_date. " $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_BODY -a ${filename} echo ${filename} fi # ------------------------------------------------ # Removing files older than $RETENTION parameter # ------------------------------------------------ find ${AWR_DIR} -name "*awrrpt*" -mtime +$RETENTION -exec rm {} \; exit
2、产生awr report 的sql脚本
robin@SZDB:~/dba_scripts/custom/awr> more autoawr_by_time.sql SET ECHO OFF; SET VERI OFF; SET FEEDBACK OFF; SET TERMOUT ON; SET HEADING OFF; SET TRIMSPOOL ON; VARIABLE rpt_options NUMBER; DEFINE no_options = 0; define ENABLE_ADDM = 8; REM according to your needs, the value can be 'text' or 'html' DEFINE report_type='html'; BEGIN :rpt_options := &no_options; END; / VARIABLE dbid NUMBER; VARIABLE inst_num NUMBER; VARIABLE bid NUMBER; VARIABLE eid NUMBER; BEGIN SELECT snap_id INTO :bid FROM dba_hist_snapshot WHERE TO_CHAR (end_interval_time, 'yyyymmddhh24') = '&1'; SELECT snap_id INTO :eid FROM dba_hist_snapshot WHERE TO_CHAR (end_interval_time, 'yyyymmddhh24') = '&2'; SELECT dbid INTO :dbid FROM v$database; SELECT instance_number INTO :inst_num FROM v$instance; END; / --print dbid; --print bid; --print eid; --print inst_num; COLUMN ext NEW_VALUE ext NOPRINT COLUMN fn_name NEW_VALUE fn_name NOPRINT; COLUMN lnsz NEW_VALUE lnsz NOPRINT; SELECT 'txt' ext FROM DUAL WHERE LOWER ('&report_type') = 'text'; SELECT 'html' ext FROM DUAL WHERE LOWER ('&report_type') = 'html'; SELECT 'awr_report_text' fn_name FROM DUAL WHERE LOWER ('&report_type') = 'text'; SELECT 'awr_report_html' fn_name FROM DUAL WHERE LOWER ('&report_type') = 'html'; SELECT '80' lnsz FROM DUAL WHERE LOWER ('&report_type') = 'text'; SELECT '1500' lnsz FROM DUAL WHERE LOWER ('&report_type') = 'html'; set linesize &lnsz; COLUMN report_name NEW_VALUE report_name NOPRINT; SELECT instance_name || '_awrrpt_' || instance_number || '_' ||'&&1'||'_'||'&&2'|| '.' || '&ext' report_name FROM v$instance a, (SELECT TO_CHAR (begin_interval_time, 'yyyymmdd') timestamp FROM dba_hist_snapshot WHERE snap_id = :bid) b; SET TERMOUT OFF; SPOOL ${AWR_DIR}/&report_name; --SPOOL &report_name SELECT output FROM TABLE (DBMS_WORKLOAD_REPOSITORY.&fn_name (:dbid, :inst_num, :bid, :eid, :rpt_options)); SPOOL OFF; SET TERMOUT ON; CLEAR COLUMNS SQL; TTITLE OFF; BTITLE OFF; REPFOOTER OFF; SET TRIMSPOOL OFF; UNDEFINE report_name UNDEFINE report_type UNDEFINE fn_name UNDEFINE lnsz UNDEFINE no_options exit;
3、补充说明
a、该脚本实现了基于不同时段,不同instance自动生成awr report,具体如下
b、用法为./autoawr_by_time.sh <instance_name> [begin time] [end time],可以用于随时随地直接生成awr report
c、在省略[begin time] [end time]的情形下会自动生成昨天凌晨至今天凌晨的awr report
d、当仅仅省略[end time]时则从[begin time]开始至当前的最大snap_id来生成awr report
e、当[begin time] [end time]都被指定时则生成指定时段的awr report
f、通过调用sendEmail发送awr report,具体参考:不可或缺的 sendEmail
4、部署参考
#如果仅仅需要一整天的awr report,直接将其部署到crontab即可。 #如果需要一整天以及不同时段的awr report,则可以考虑采用如下方式来部署,将其合并到一个shell文