2008-08-05 11:07:15
 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。

2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:
select id from t where num is null
可以在num上设置默认值0,确保表中num列没有null值,然后这样查询:
select id from t where num=0

3.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描。

4.应尽量避免在 where 子句中使用 or 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描,如:
select id from t where num=10 or num=20
可以这样查询:
select id from t where num=10
union all
select id from t where num=20

5.in 和 not in 也要慎用,否则会导致全表扫描,如:
select id from t where num in(1,2,3)
对于连续的数值,能用 between 就不要用 in 了:
select id from t where num between 1 and 3

6.下面的查询也将导致全表扫描:
select id from t where name like '%abc%'
若要提高效率,可以考虑全文检索。

7.如果在 where 子句中使用参数,也会导致全表扫描。因为SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。如下面语句将进行全表扫描:
select id from t where num=@num
可以改为强制查询使用索引:
select id from t with(index(索引名)) where num=@num

8.应尽量避免在 where 子句中对字段进行表达式操作,这将导致引擎放弃使用索引而进行全表扫描。如:
select id from t where num/2=100
应改为:
select id from t where num=100*2

9.应尽量避免在where子句中对字段进行函数操作,这将导致引擎放弃使用索引而进行全表扫描。如:
select id from t where substring(name,1,3)='abc'--name以abc开头的id
select id from t where datediff(day,createdate,'2005-11-30')=0--‘2005-11-30’生成的id
应改为:
select id from t where name like 'abc%'
select id from t where createdate>='2005-11-30' and createdate<'2005-12-1'

10.不要在 where 子句中的“=”左边进行函数、算术运算或其他表达式运算,否则系统将可能无法正确使用索引。

11.在使用索引字段作为条件时,如果该索引是复合索引,那么必须使用到该索引中的第一个字段作为条件时才能保证系统使用该索引,否则该索引将不会被使用,并且应尽可能的让字段顺序与索引顺序相一致。

12.不要写一些没有意义的查询,如需要生成一个空表结构:
select col1,col2 into #t from t where 1=0
这类代码不会返回任何结果集,但是会消耗系统资源的,应改成这样:
create table #t(...)

13.很多时候用 exists 代替 in 是一个好的选择:
select num from a where num in(select num from b)
用下面的语句替换:
select num from a where exists(select 1 from b where num=a.num)

14.并不是所有索引对查询都有效,SQL是根据表中数据来进行查询优化的,当索引列有大量数据重复时,SQL查询可能不会去利用索引,如一表中有字段sex,male、female几乎各一半,那么即使在sex上建了索引也对查询效率起不了作用。

15.索引并不是越多越好,索引固然可以提高相应的 select 的效率,但同时也降低了 insert 及 update 的效率,因为 insert 或 update 时有可能会重建索引,所以怎样建索引需要慎重考虑,视具体情况而定。一个表的索引数最好不要超过6个,若太多则应考虑一些不常使用到的列上建的索引是否有必要。

16.应尽可能的避免更新 clustered 索引数据列,因为 clustered 索引数据列的顺序就是表记录的物理存储顺序,一旦该列值改变将导致整个表记录的顺序的调整,会耗费相当大的资源。若应用系统需要频繁更新 clustered 索引数据列,那么需要考虑是否应将该索引建为 clustered 索引。

17.尽量使用数字型字段,若只含数值信息的字段尽量不要设计为字符型,这会降低查询和连接的性能,并会增加存储开销。这是因为引擎在处理查询和连接时会逐个比较字符串中每一个字符,而对于数字型而言只需要比较一次就够了。

18.尽可能的使用 varchar/nvarchar 代替 char/nchar ,因为首先变长字段存储空间小,可以节省存储空间,其次对于查询来说,在一个相对较小的字段内搜索效率显然要高些。

19.任何地方都不要使用 select * from t ,用具体的字段列表代替“*”,不要返回用不到的任何字段。

20.尽量使用表变量来代替临时表。如果表变量包含大量数据,请注意索引非常有限(只有主键索引)。

21.避免频繁创建和删除临时表,以减少系统表资源的消耗。

22.临时表并不是不可使用,适当地使用它们可以使某些例程更有效,例如,当需要重复引用大型表或常用表中的某个数据集时。但是,对于一次性事件,最好使用导出表。

23.在新建临时表时,如果一次性插入数据量很大,那么可以使用 select into 代替 create table,避免造成大量 log ,以提高速度;如果数据量不大,为了缓和系统表的资源,应先create table,然后insert。

24.如果使用到了临时表,在存储过程的最后务必将所有的临时表显式删除,先 truncate table ,然后 drop table ,这样可以避免系统表的较长时间锁定。

25.尽量避免使用游标,因为游标的效率较差,如果游标操作的数据超过1万行,那么就应该考虑改写。

26.使用基于游标的方法或临时表方法之前,应先寻找基于集的解决方案来解决问题,基于集的方法通常更有效。

27.与临时表一样,游标并不是不可使用。对小型数据集使用 FAST_FORWARD 游标通常要优于其他逐行处理方法,尤其是在必须引用几个表才能获得所需的数据时。在结果集中包括“合计”的例程通常要比使用游标执行的速度快。如果开发时间允许,基于游标的方法和基于集的方法都可以尝试一下,看哪一种方法的效果更好。

28.在所有的存储过程和触发器的开始处设置 SET NOCOUNT ON ,在结束时设置 SET NOCOUNT OFF 。无需在执行存储过程和触发器的每个语句后向客户端发送 DONE_IN_PROC 消息。

29.尽量避免大事务操作,提高系统并发能力。

30.尽量避免向客户端返回大数据量,若数据量过大,应该考虑相应需求是否合理
2008-07-25 10:58:33
在JavaScript中,进行数学运算时,经常出现比较奇怪的现象,如1.5 * 1.2 =1.79999998之类的问题,主要是由于计算机的二进制表示丢失精度。
 
能否解决这个问题呢,我们知道在整数进行运算时,不会出现二进制表示丢失精度的问题,因此我们可以用整数运算代替浮点运算。
 
用下面的add/sub/mult/div方法替代+-*/。
///<remarks>
/// Create Author: Bruce
/// Create Date  : 07/25/2008 09:31:11
/// Create Reason: fix float operation loss precision problems
///</remarks>
Number.prototype.getB = function(){
var arr = this.toString().split('.');
return arr[1]? arr[1].length : 0;
}
Number.prototype.getP = function(to){
return Math.pow(10, Math.max(this.getB(), to.getB()));
}
Number.prototype.add = function(to){
var p = this.getP(to);
return (Math.round(this * p) +Math.round(to * p)) / p;
}
Number.prototype.sub = function(to){
var p = this.getP(to);
return (Math.round(this * p) -Math.round(to * p)) / p;
}
Number.prototype.mult= function(to){
var p1 = this.getP(0); 
var p2 = to.getP(0);
return Math.round(this * p1)* Math.round(to* p2)/p1/p2;
}
Number.prototype.div= function(to) {
 var p= this.getP(to); 
 return (Math.round(this*p) /Math.round(to*p));
}
2008-07-01 10:31:57
在Javascript中对于对象的判断,一般可以判断是否为空,但对于函数好像不能直接判断。在网上找了一下,发现以下方法可行:
 
判断对象:
var o=document.getElementById("xxxx");
if (o)
{
   ....
}
 
判断函数:
if (typeof(window.refreshTO)=="function")
{
  window.refreshTO();
}
2008-06-20 11:28:40
  • 事件源对象
    event.srcElement.tagName
    event.srcElement.type
  • 捕获释放
    event.srcElement.setCapture(); 
    event.srcElement.releaseCapture(); 
  • 事件按键
    event.keyCode
    event.shiftKey
    event.altKey
    event.ctrlKey
  • 事件返回值
    event.returnValue
  • 鼠标位置
    event.x
    event.y
  • 窗体活动元素
    document.activeElement
  • 绑定事件
    document.captureEvents(Event.KEYDOWN);
  • 访问窗体元素
    document.all("txt").focus();
    document.all("txt").select();
  • 窗体命令
    document.execCommand
  • 窗体COOKIE
    document.cookie
  • 菜单事件
    document.oncontextmenu
  • 创建元素
    document.createElement("SPAN"); 
  • 根据鼠标获得元素:
    document.elementFromPoint(event.x,event.y).tagName=="TD
    document.elementFromPoint(event.x,event.y).appendChild(ms) 
  • 窗体图片
    document.images[索引]
  • 窗体事件绑定
    document.onmousedown=scrollwindow;
  • 元素
    document.窗体.elements[索引]
  • 对象绑定事件
    document.all.xxx.detachEvent('onclick',a);
  • 插件数目
    navigator.plugins
  • 取变量类型
    typeof($js_libpath) == "undefined"
  • 下拉框
    下拉框.options[索引]
    下拉框.options.length
  • 查找对象
    document.getElementsByName("r1");
    document.getElementById(id);
  • 定时
    timer=setInterval('scrollwindow()',delay);
    clearInterval(timer);
  • UNCODE编码
    escape() ,unescape
  • 父对象
    obj.parentElement(dhtml)
    obj.parentNode(dom)
  • 交换表的行
    TableID.moveRow(2,1)
    document.all.csss.href = "a.css"; display:inline style="word-break:break-all" obj.scrollIntoView(true)
  • 替换CSS
  • 并排显示
  • 隐藏焦点
    hidefocus=true
  • 根据宽度换行
  • 自动刷新
    <meta HTTP-EQUIV="refresh" CONTENT="8;URL=http://c98.yeah.net">
  • 简单邮件
    <a  href="
    mailto:aaa@bbb.com?subject=ccc&body=xxxyyy"> 
  • 快速转到位置

  • <a name="first">
    <a href="#first">anchors</a>
  • 网页传递参数
    location.search();
  • 可编辑
    obj.contenteditable=true
  • 执行菜单命令
    obj.execCommand
  • 双字节字符
    /[^\x00-\xff]/
    汉字
    /[\u4e00-\u9fa5]/
  • 让英文字符串超出表格宽度自动换行
    word-wrap: break-word; word-break: break-all;
  • 透明背景
    <IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe>
  • 获得style内容
    obj.style.cssText
  • HTML标签
    document.documentElement.innerHTML
  • 第一个style标签
    document.styleSheets[0]
  • style标签里的第一个样式
    document.styleSheets[0].rules[0]
  • 防止点击空链接时,页面往往重置到页首端。
    <a href="javascript:function()">word</a>
  • 上一网页源
    asp:
    request.servervariables("HTTP_REFERER")
    javascript:
    document.referrer
  • 释放内存
    CollectGarbage();
  • 禁止右键
    document.oncontextmenu = function() { return false;}
  • 禁止保存
    <noscript><iframe src="*.htm"></iframe></noscript>
  • 禁止选取<body oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false"onmouseup="document.selection.empty()> 
  • 禁止粘贴
    <input type=text onpaste="return false">
  • 地址栏图标
    <link rel="Shortcut Icon" href="favicon.ico">
    favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下
  • 收藏栏图标
    <link rel="Bookmark" href="favicon.ico">
  • 查看源码
    <input type=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http://www.csdn.net/'">
  • 关闭输入法
    <input style="ime-mode:disabled">
  • 自动全选
    <input type=text name=text1 value="123" onfocus="this.select()">
  • ENTER键可以让光标移到下一个输入框
    <input onkeydown="if(event.keyCode==13)event.keyCode=9">
  • 文本框的默认值
    <input type=text value="123" onfocus="alert(this.defaultValue)">
  • title换行
    obj.title = "123&#13sdfs&#32"
  • 获得时间所代表的微秒
    var n1 = new Date("2004-10-10".replace(/-/g, "\/")).getTime()
  • 窗口是否关闭
    win.closed
  • checkbox扁平
    <input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"><br>
  • 获取选中内容
    document.selection.createRange().duplicate().text 
  • 自动完成功能
    <input  type=text  autocomplete=on>打开该功能 
    <input  type=text  autocomplete=off>关闭该功能  
  • 一旦对象的属性发生改变,立即触发事件
    <input type=text  onpropertychange ="javascript:alert('Hi')">   
  • 窗口最大化
    <body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)">
  • 无关闭按钮IE
    window.open("aa.htm", "meizz", "fullscreen=7");
  • 统一编码/解码
    alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))
    encodeURIComponent对":"、"/"、";" 和 "?"也编码
  • 表格行指示
    <tr onmouseover="this.bgColor='#f0f0f0'" onmouseout="this.bgColor='#ffffff'">
  • 获取在父文档中生成 window 的 frame 或 iframe 对像
    window.frameElement
  • 获取当前对象的内置window对象,作用于frame或iframe
    document.all.object.contentWindow
  • 连接是否访问过
    event.srcElement.currentStyle.visited
  • 组件是否安装
    clientCaps.isComponentInstalled("{6B053A4B-A7EC-4D3D-4567-B8FF8A1A5739}", "componentID"))
  • 2008-06-06 15:56:54
     
    Email:          \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
     
    Int :           [0-9]{0,16}
     
    Numeric(23,6):([0-9]{1,16}(\.[0-9]{1,2})?)?
     
    Multiple Line TextBox(500 Char): (.|\r|\n){0,500}
    2008-05-23 09:25:00
        declare  @name nvarchar(4000)
    用CURSOR实现:    
        declare  @tname nvarchar(150)  
        /*
        Declare myCur cursor for Select VendorName
        */
        declare myCur cursor for Select VendorName
        from dbo.StopNotice As C,dbo.Vendor As D
        Where  C.StopNoticeStatus=1 And   C.SubmitedBy=D.VendorID and C.TaskOrderID=@taskOrderID
        Select @name='' 
        open myCur
        -- Fetch Each Sub Vendor Names, Added To Return Value
        fetch from myCur into @tname
        while (@@fetch_status=0)
        begin  
           select @name=@tname+';'+@name
           fetch next from myCur into  @tname
        end
        close myCur
        deallocate myCur
    另类SQL实现:
        Select @name='';
        Select @name=VendorName+';'+@name
        from dbo.StopNotice As C,dbo.Vendor As D
        Where  C.StopNoticeStatus=1 And C.SubmitedBy=D.VendorID and C.TaskOrderID=@taskOrderID
      
     
    2008-05-17 10:06:09
    2005/05/15
     
    最近锻炼宝宝爬行,把他放到床上,由于找不到受力点,对前面的手机很有兴趣,也没有办法。
     
    9个月的孩子应该学会爬行的,实在找不到合适的地方让孩子练习爬行,床上太软了,没办法用力,每次宝宝都是努力一阵子没有成功,就放弃了。
     
    诶,今天宝宝找到办法了。他先用手把自己撑起来,向前倾,然后用力往前甩,成功了。乐得我与孩子他妈都笑了。想起魔戒三部曲的那个小矮人,需要别人把他丢过去。今天宝宝把自己往前丢,以丢代爬。
     
     
    2008-05-17 09:58:19
    2008/05/16
     
    今天收到中国银行的贷款偿还列表,说明贷款已发放到房东帐户了。
     
    中介约定明天交房,进行必要的费用清理。
     
    我们自己的房子,新房子里面宽敞一些,宝宝的活动空间大多了。
    2008-05-11 16:53:28
    2008年5月6日,网上房地产查询,产证已办理好,联系中介。
     
    2008年5月7日,中介拿到房产证,帮忙办理维修基金过户。
     
    2008年5月9日,拿到房产证,派出所查询户口事项,一切正常。
     
    2008年5月11日,再次看房子,设计装修。
    2008-05-11 16:50:32
    最近黄感冒咳嗽,已经好几天了。
     
    昨天发现宝宝故意学咳嗽来引起大人的注意,我们看着都觉得很好玩的。
     
    现在宝宝开始有脾气了,他不高兴的时候,会用手使劲的抓我的脸与脖子,还比较疼的。
     
    宝宝慢慢的长大了!
    当前 1页/4页 首 页 下一页 末 页