oracle order by 求帮优化

select A.aId, (select count(1) from B where B.aId = a.aId) c from A
order by c

表A有30W+数据
表B有18W+数据
用上面sql查要10分钟,如果去掉order by则在2秒内,求高手指导怎么优化

4个回答

有高人指点,这样写能控制在10秒左右,但不是理解
select A.aId, NVL(B.c,0) from A LEFT JOIN (select aId,count(1) c from B group by aId) B ON A.AID=B.AID
order by B.c

我没看explain plan但是我猜你的SQL首先不太对(我假设B.aId,A.aId有index)

SELECT aId, 1 FROM
select DISTINCT(A.aId, A.c) from A, B where B.aId = a.aId D
ored by c

eagleyan
Coursera SELECT aId, 1 FROM select DISTINCT(A.aId, B.c) from A, B where B.aId = a.aId D ored by c
接近 5 年之前 回复
lnkToKing
lnkToKing 回复lnkToKing: (select count(1) from B where B.aId = a.aId) c
接近 5 年之前 回复
lnkToKing
lnkToKing A表没有c字段,c字段是b表统计出来的
接近 5 年之前 回复

在 A.aId 上建索引
必要时在 B.aId 也建索引

c 是计算字段,不宜用于排序
可尝试
select *
from
(select A.aId, (select count(1) from B where B.aId = a.aId) c from A) T
order by c

lnkToKing
lnkToKing 这写法试过了,效率没差多少
接近 5 年之前 回复

具体情况不了解,试试这样看能不能快点!
select a.aId,
sum(case when B.aId is not null then 1 else 0 end) tot
from A left join B
on B.aId = a.aId
group by a.aId;

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
oracle order by 查询要10分钟 求帮优化
select A.aId, (select count(1) from B where B.aId = a.aId) c from Arnorder by crn表A有30W+数据rn表B有18W+数据rn用上面sql查要10分钟,如果去掉order by则在2秒内,求高手指导怎么优化
oracle order by 优化
不加红色字体语句,查询很快,但是开发中有不能不加,有没方法优化rnSELECT pid,pdepartment,ptype,pstation,pstarttime,pstoptime,pline,pposition,pequipment,pscope,preason rnFROM ( SELECT pid,pdepartment,ptype,pstation,pstarttime,pstoptime,pline,pposition,pequipment,pscope,preason,rownum rn rn FROM ( SELECT pid,pdepartment,ptype,pstation,pstarttime,pstoptime,pline,pposition,pequipment,pscope,preason rn FROM (SELECT pid,pdepartment,ptype,pstation,pstarttime,pstoptime,pline,pposition,pequipment,pscope,preason rn FROM Powinfcol rn where 1=1 and pDepartment = '1' and pType = '1' and pStarttime >= to_date('2017-12-12','yyyy-mm-dd') and pStarttime <= to_date('2017-12-14','yyyy-mm-dd')) rn [color=#FF0000] ORDER BY pStarttime desc)[/color] rn WHERE rownum <= 30) rnWHERE rn>0rnrn
求一个ORDER BY 优化
select (a+b+c*d-e) from t order by (a+b+c*d-e)rn(a+b+c*d-e)内容不定rn
有个sql语句求优化,order by
[code=SQL]rnrnUSE [HJGINFODB]rnGOrn/****** 对象: Table [dbo].[TB_IP_DUAN] 脚本日期: 09/28/2010 09:56:38 ******/rnSET ANSI_NULLS ONrnGOrnSET QUOTED_IDENTIFIER ONrnGOrnCREATE TABLE [dbo].[TB_IP_DUAN](rn [id] [int] IDENTITY(1,1) NOT NULL,rn [CITY_ID] [int] NOT NULL,rn [StartIP] [bigint] NOT NULL,rn [EndIP] [bigint] NOT NULL,rn CONSTRAINT [PK_TB_IP_DUAN] PRIMARY KEY CLUSTERED rn(rn [id] ASCrn)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]rn) ON [PRIMARY]rnrnrnrnselect top 1 (StartIP-124115011056) as tt,CITY_ID from TB_IP_DUAN rnwhere (StartIP-124115011056)>0rnorder by tt ascrnrn[/code]rnrn请问,这个表的索引改如何设计,现在这个sql语句消耗性能比较高rnrn测试通过给满分rn
求大神帮优化SQL
[code=sql]rnselect a.id,rn a.name,rn sum(case when n.voltagelevelid = 35 then e.q else 0 end) as sumq500,rn sum(case when n.voltagelevelid = 33 then e.q else 0 end) as sumq220,rn sum(case when n.voltagelevelid = 32 then e.q else 0 end) as sumq110,rn sum(case when n.voltagelevelid = 25 then e.q else 0 end) as sumq35rn from hissubstationmeasure ern left join tblsubstation nrn on e.id = n.idrn left join tblsubcontrolarea arn on n.subcontrolareaid = a.idrn where n.used = 1rn and e.dtime betweenrn to_date('2013-12-31 00:00:00', 'yyyy-MM-dd hh24:mi:ss') andrn to_date('2013-12-31 23:59:59', 'yyyy-MM-dd hh24:mi:ss')rn group by a.id, a.name;rn[/code]rn[code=sql]rnSELECT STATEMENT, GOAL = ALL_ROWS 8488 40 4440rn HASH GROUP BY 8488 40 4440rn TABLE ACCESS BY GLOBAL INDEX ROWID SDWGDY HISSUBSTATIONMEASURE 43 258 19092rn NESTED LOOPS 8474 100243 11126973rn NESTED LOOPS OUTER 7 388 14356rn TABLE ACCESS FULL SDWGDY TBLSUBSTATION 5 388 9700rn TABLE ACCESS BY INDEX ROWID SDWGDY TBLSUBCONTROLAREA 1 1 12rn INDEX UNIQUE SCAN SDWGDY PK_TBLSUBCONTROLAREA 0 1 rn INDEX RANGE SCAN SDWGDY INDEX_2202 2 83 rn[/code]rnselect count(*) from hissubstationmeasure ;rn7311213rnselect count(*) from tblsubstation;rn938rnselect count(*) from tblsubcontrolarea;rn9rn这样的查询需要30秒rn其中对表 建立 ID,DTIME,AREAID 索引rn对tblsubstation 建立唯一索引IDrn对tblsubcontrolarea 建立唯一索引ID
求大神帮优化下查询
[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/onion/30.gif][/img]求大神们帮小弟优化下sql,现在查询一条要7s,简直要了我的老命。上代码。rnrn[code=sql]rnselect su.id as poolid, su.su_name as cnName, sum(c.count) as countrn FROM (select distinct jet.office, jet.appname, companyidrn from ir biguserrn join ip apprn on biguser.user_id = app.user_idrn and app.invalid = 0rn and biguser.invalid = 0rn join it jetrn on app.jet_id = jet.jet_idrn and jet.ilid = 0) usersrn join absu surn on su.id || '' = users.companyidrn join IBL prn on upper(p.enname) = upper(users.appname)rn and upper(p.office) = upper(users.office)rn join (select * from IT where cmd <> 'Exception' and logdate between to_date(20140101, 'yyyymmdd') andrn to_date(20140910, 'yyyymmdd')) crn on c.poolid = p.poolidrn join IE cmdrn on cmd.cmd_id = RTRIM(c.cmd || '_' || c.subcmd1, '_')rn group by su.id, su.su_namern order by count descrnrn[/code]
求Oracle递归查询的优化
在树结果中,别人用到Oracle用Start with...Connect By子句递归查询,大家有什么好的优化方案?谢谢!rn如 select * from persons.dept start with deptid=xx connect by prior paredeptid=deptid
SQL表连接、 order by 优化
select * from (select t2.* from (select rownum r, t1.* from (Select * From (Select * From crm_Vd健康档案 Where 1 = 1 And 有效状态b = '1') T order by 显示顺序, 系统序号) t1 where rownum &amp;lt;= 100) t2 where t2.r &amp;gt; 0...
mysql order by 优化
1.一些简单sql调优n 2.复杂sql的调优
mysql 关于order by的优化
explain SELECT m.*,u.username nickname,u.vip,u.appid FROM wwj_mingxi m left join wwj_user u on u.id=m.uid WHERE u.appid = 0 ORDER BY m.id desc LIMIT 0,20 花费时间4s 可以看出主要原因是使用了临时表,文件排序。也就是说order
关于sql优化 之 order by
[code=SQL]rnrnSELECT ip, COUNT(ip) ip_count FROM ip_log GROUP BY ip ORDER BY ip_count descrnrnrnSELECT ip, COUNT(ip) FROM ip_log GROUP BY ip ORDER BY COUNT(ip) descrnrn[/code]rnrn请问一下 这两条sql的效率相差大么??rnrn有多大?rnrn还有可以优化的余地了么?rnrn谢谢大家
order by语句怎么优化
select *rn from (select OBJUID,rn XMMC,rn dt_broadcast,rn ggtype,rn Row_Number() over(order by dt_broadcast desc) rownum_rn from (select OBJUID, na_item_name XMMC, dt_broadcast, 1 ggtypern from db_perambulate_item_broadcast trn union allrn select OBJUID, na_mine_name XMMC, dt_broadcast, 2 ggtypern from db_mining_apply_broadcast t))rn where rownum <7rnrn这个数据正常执行应该是0.3秒这样的,但是系统执行都要2秒多,要怎么优化呢rndt_broadcast desc 这个字段是时间类型的,没有NULL值,用了位图索引rndb_perambulate_item_broadcast 67840条数据rndb_mining_apply_broadcast 168428条数据
mysql order by/group by 优化
explain select im.item_id from property_value p left join item i on p.id = i.value left join rnitem_message im on i.item_id = im.item_id where ( p.value like '%餐饮%' or p.value like '%拉%' ) and im.class = 1 and im.status = 0 and p.`property_id` in (SELECT id FROM rn`property` WHERE `key_p` = 1) LIMIT 24 , 8rnrnrnid select_type table type possible_keys key key_len ref rows Extra rn1 PRIMARY im ref class,item_id class 4 const 9096 Using where rn1 PRIMARY i ref value,item_id item_id 4 fenlei.im.item_id 8 Using where rn1 PRIMARY p eq_ref PRIMARY PRIMARY 4 fenlei.i.value 1 Using where rn2 DEPENDENT SUBQUERY property unique_subquery PRIMARY PRIMARY 4 func 1 Using where rnrnrnexplain select count(im.item_id), im.item_id from property_value p left join item i on p.id = i.value left join rnitem_message im on i.item_id = im.item_id where ( p.value like '%餐饮%' or p.value like '%拉%' ) and im.class = 1 and im.status = 0 and p.`property_id` in (SELECT id FROM rn`property` WHERE `key_p` = 1) group by im.item_id LIMIT 20 , 8rnrnid select_type table type possible_keys key key_len ref rows Extra rn1 PRIMARY im index class,item_id item_id 4 NULL 3 Using where rn1 PRIMARY i ref value,item_id item_id 4 fenlei.im.item_id 8 Using where rn1 PRIMARY p eq_ref PRIMARY PRIMARY 4 fenlei.i.value 1 Using where rn2 DEPENDENT SUBQUERY property unique_subquery PRIMARY PRIMARY 4 func 1 Using where rnrnexplain select count(im.item_id), im.item_id from property_value p left join item i on p.id = i.value left join rnitem_message im on i.item_id = im.item_id where ( p.value like '%餐饮%' or p.value like '%拉%') and im.class = 1 and im.status = 0 and p.`property_id` in (SELECT id FROM rn`property` WHERE `key_p` = 1) group by im.item_id order by count(im.item_id) LIMIT 20 , 8rnrnid select_type table type possible_keys key key_len ref rows Extra rn1 PRIMARY im index class,item_id item_id 4 NULL 3 Using where; Using temporary; Using filesort rn1 PRIMARY i ref value,item_id item_id 4 fenlei.im.item_id 8 Using where rn1 PRIMARY p eq_ref PRIMARY PRIMARY 4 fenlei.i.value 1 Using where rn2 DEPENDENT SUBQUERY property unique_subquery PRIMARY PRIMARY 4 func 1 Using where rnrn我要用得第3条sql,我要order by的是count(im.item_id)这个数,请问如何优化,附索引,谢谢rnrnSHOW INDEX FROM item_message rnrnTable Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment rnitem_message 0 PRIMARY 1 id A 11994 NULL NULL BTREE rnitem_message 1 class 1 class A 3 NULL NULL BTREE rnitem_message 1 regional 1 regional A 3 NULL NULL YES BTREE rnitem_message 1 city 1 city A 11 NULL NULL YES BTREE rnitem_message 1 regional_city 1 regional A 3 NULL NULL YES BTREE rnitem_message 1 regional_city 2 city A 11 NULL NULL YES BTREE rnitem_message 1 create_date 1 create_date A 3 NULL NULL YES BTREE rnitem_message 1 item_id 1 item_id A 11994 NULL NULL BTREE rnrnSHOW INDEX FROM item rnrnTable Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment rnitem 0 PRIMARY 1 id A 1148679 NULL NULL BTREE rnitem 0 value 1 value A 1148679 NULL NULL BTREE rnitem 1 item_id 1 item_id A 143584 NULL NULL BTREE rnitem 1 class_id 1 class_id A 3 NULL NULL BTREE rnrnSHOW INDEX FROM property_value rnrnTable Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment rnproperty_value 0 PRIMARY 1 id A 1149757 NULL NULL BTREE rnproperty_value 1 property_id 1 property_id A 23 NULL NULL BTREE rnproperty_value 1 property_id 2 value A 6495 255 NULL BTREE rnproperty_value 1 value 1 value NULL 4892 NULL NULL FULLTEXT rnrnrn
mysql order by 原理和优化
原理 利用索引的有序性获取有序数据 利用内存/磁盘文件排序获取结果 1) 双路排序:是首先根据相应的条件取出相应的排序字段和可以直接定位行数据的行指针信息,然后在sort buffer 中进行排序。 2)单路排序:是一次性取出满足条件行的所有字段,然后在sort buffer中进行排序。 优化方式 给order by 字段增加索引,orderby的字段必须在最前面设置 接下来给来说一下ord
MySQL之order by 优化(1)
在多数情况下,可以通过索引来简化order by的操作,而不需要额外的using filesort来重新进行排序,但是order by中出现的排序的列需要满足最左前缀索引, 注意(个人实操结果):当所选取的列,即select中出现的列不出现在order by中索引的时候,则不会对ordr by 进行优化,即所选取的列与order by待排序的列是一致的,不然仍然会使用using filesort
怎么优化 order by???急
我的机器环境 PII400/64M+WIN98+ORACLE PERSONAL 8.0.4rn SGA 15M 主要数据表数据量 16万条rn与一个几百条数据的表做连接查询,并且要按照大表的部分字段‘order by’速度rn无法忍受,要等近10分钟,如何解决,各位高人请指点!!!rn rn
求ORACLE代码?有关COUNT、GROUP BY、ORDER BY
表VISITORrnVISITORID RAW(32)rnCLASSCODE RAW(32)rn表CODECLASSrnCLASSCODE RAW(32)rnCLASSNAME VARCHAR2(32)rnSERIALNUMBER NUMBER(3,0)rnrn要求:rn1-按照CLASSCODE进行GROUP BYrn2-用COUNT统计VISITORIDrn3-按照SERIALNUMBER进行ORDER BYrn4-SELECT内容为CODECLASS.CLASSNAME, COUNT(VISITOR.VISITORID)rnrn敬请指教rn
MySQL Query 的优化— ORDER BY 的实现与优化
mysql、优化、order by
MySQL中队order by的优化(优化2)
MySQL有两种排序方式:通过所以扫描直接返回有序的数据,先返回无序数据,后对数据进行排序(filesort)。 第一种方式:前提条件索引必须是有序的,在查询时,where条件和order by 使用相同的索引,而且order by和索引的顺序一致。否则就会filesort,在SQL中使用索引的情况有: (1),select *from table_name order by key, (2),s...
oracle order by 降序不了
一张表,共5000条数据左右。字段:id,namernid 是自增idrnname 是名称rnrn如何通过id降序rnselect id,name from tb order by id decs,rn这样出来的每次都是前22数据,虽然是id降序。并不是整张表所有数据的通过id的降序。rn请帮忙rnrn不好意思 发帖提示分不够40,谢谢大家帮忙
oracle order by 排序问题~~~
一张表,有这3个字段:id,name,typernid 是随机生成的唯一,因是随机生成的,所以很难确定其具体值。rnname 是名称,不唯一rntype 是类型rnrn我新加入了一条记录(暂定名:A)进去,然后用select * from table 查询,发现A记录没有在第一行,我希望把A记录放在第一行,怎么操作?高手请指点,非常感谢,一有好答案马上结贴~!!rnrn之前用过rownum ; rowid,但都无济于事。
Oracle之分析函数 之 order by子句
/* 有ORDER BY的存在将添加一个默认的开窗子句!意味着从第一行到当前行; 没有ORDER BY时,默认的窗口是全部的分区 ; 在Order by 子句后可以添加nulls last,如:order by comm desc nulls last   表示排序时忽略comm列为空的行.     不写between AND  ,在有order BY 的情况下,就是分组第一行
oracle order by问题求助
问一个oracle order by的问题,rn网上看到oracle的order by是按照字符的ascii大小排序的.rnrn当前表结构如下.rn[code=sql]rnPATH AMOUNTrn1 2rn2 2rn5 2rn8 2rn1,3,4 3rn1,7 3rn2,7 3rn5,7 3rn7,8 3rn[/code]rn现在对于amount相等的时候,想实现如下目的,rn1, 单独的先挑出来,例如其中的amount=2 ,那么先选 path= 1的rn2, 如果没有单独的,例如amount=3,这种情况就选1,7 (也就是前面的,组合最少的优先)rnrn不知道oracle如何实现这样的要求,应为我想实现这个,然后嵌套一层rownum =1找出需要的那一条.
oracle order by问题
select t.name as 名称,rn nvl(t.face, 0) as 面值rnfrom st_order t group t by t.name,t.facernorder by casern when v_orderby = 1 thenrn 名称rn when v_orderby = 2 thenrn 面值rnendrnrnrn面值是number类型,名称是char ,这个面值排序有问题,to_char后排出来的不对,谁能帮解决一下
oracle的order by排序规则
在oracle的sql中,order by排序时,如果两个或多个行字段值相同的话,oracle以什么样的规则对这些值进行排序呢?是按照索引吗?rn比如:从用户表查询,根据年龄排序:rnselect * from plt_user order by age.rn如果两个或多个用户年龄相同的话,oracle是怎么排序呢
oracle自表关联求优化
数据量200W。。rn需要取用户最迟时间的那条记录rnrn[img=https://img-bbs.csdn.net/upload/201509/24/1443063442_465270.png][/img]
ORACLE删除重复记录求优化
DELETErn FROM "table"rn WHERE "RowID" NOT IN (SELECT MAX("RowID")rn FROM "table"rn GROUP BYrn "aaa",rn "bbb",rn "ccc"rn HAVING COUNT(*) >= 1);rnrn很普通的一条去重复语句,但是当数据量大时(两三万条记录),执行时间要几个小时,但这时执行括号内的查询很快rn求优化方案
求oracle动态SQL优化方案
这里有8张表,每张表的数据量都是百万级。表间关系确定,但是存在1对1和1对N的情况。用户可以选择查看任意表的任意列,可以将任何一个或多个字段设为where条件。具体场景如下:rn 一共是A、B、C、D、E、F、G、H表,用户先选择查看A表的a1、a2和B表的b1、b2字段,where条件是a3='1',b3='2'.将查询结果分页显示给客户。客户此基础上,要再看看D表的d1字段,于是增加条件d3='4'。如此分步查询,最后可能将8张表的数据都查出来,分页显示。rn 因为这里没有主次表之分,所以不存在左、右连接的问题。每条数据都必须满足所有的where条件。rn 索引之类的都已建好,常说的不用like、in之类的方法是不行的,有没有方法在sql上进行优化。
update oracle 不能order by?
大家好 我想实现的是有个表arn字段 state createdaternrn里面有很多数据 我想修改按照createdate 排序的前100条记录的 staternupdate table a set state='1' wherr rownum<100 order by createdaternrn这条肯定通不过的 该怎么实现? 请教ing 先谢大家了
【oracle】中文的order by排序规则
总结: 单纯使用order by排序中文,是根据ascii码排序的,不是根据拼音排序的。 如果要根据拼音排序某一列,需要用函数nlssort(列名,’NLS_SORT=SCHINESE_PINYIN_M’) 传送门: https://jingyan.baidu.com/article/d621e8da17f00c2865913fd4.html...
oracle OTM order management
oracle transportation management, order management functionality
oracle 子查询 order by 问题
select * from A where id in (select aid from B where rownum<2 order by create_time desc)rn我想根据某个字段排序后的取第一条数据,怎么报错呢,报:缺少右括号;rn本人对oracle不是太精通,请各位大侠们帮帮忙!!急啊,
SQL语句优化过程策略,帮助您优化Oracle查询语句
SQL语句优化过程优化策略 21. /*+ORDERED*/ 根据表出现在FROM中的顺序,ORDERED使ORACLE依此顺序对其连接. 例如: SELECT /*+ORDERED*/ A.COL1,B.COL2,C.COL3 FROM TABLE1 A,TABLE2 B,TABLE3 C WHERE A.COL1=B.COL1 AND B.COL1=C.COL1; 22. /*+USE_NL(TABLE)*/ 将指定表与嵌套的连接的行源进行连接,并把指定表作为内部表. 例如: SELECT /*+ORDERED USE_NL(BSEMPMS)*/ BSDPTMS.DPT_NO,BSEMPMS.EMP_NO,BSEMPMS.EMP_NAM FROM BSEMPMS,BSDPTMS WHERE BSEMPMS.DPT_NO=BSDPTMS.DPT_NO; 23. /*+USE_MERGE(TABLE)*/
Mysql随机取样——ORDER BY RAND()优化
The palest ink is better than best memory——好记性不如烂笔头。2013补记 一、关键词:随机取样、order by rand()二、业务场景:一款新产品上线后,刚开始用户比较少,不够活跃。但,出于运营需要,比如社交产品首页Feed流随机出老动态,系统修改发布时间显示~电商产品的商品列表随机显示商品,不至于每次用户看见的商品都一样——空城计——一种活跃的假象。
MySQL数据库SQL优化之order by 语句
优化ORDER BY 语句之前,首先来了解一下MySQL中排序方式。先看customer 表上的索引情况 mysql&amp;gt; show index from customer; +----------+------------+-------------------+--------------+-------------+-----------+-------------+---------...
【mysql】order by 优化与索引的应用
在某些场景,在不做额外的排序情况下,MySQL 可以使用索引来满足 ORDER BY 子句的优化。虽然 ORDER BY并不完全精确地匹配索引,但是索引还是会被使用,只要在WHERE子句中,所有未被使用的那部分索引(一个索引多个字段的情况)以及所有ORDER BY字段都是一个常量就没问题。下面这些查询语句,使用了索引来对 ORDER BY部分进行优化: SELECT * FROM t
一个MySQL order by优化案例
  今天一个用户反馈SQL执行比较慢记录下分析过程:  1.用户的SQL      SELECT * FROM ie_article_25 WHERE status = 3  AND addtime &amp;lt; 1284078469 order by addtime desc LIMIT 0, 1 \G    2. 查看执行计划    mysql&amp;gt; explain SELECT ...
mysql 带条件的 order by 优化
1.一些简单sql调优n 2.复杂sql的调优
mysql中order by实现与优化
在MySQL 中,ORDER BY 的实现有如下两种类型:  一种是通过有序索引而直接取得有序的数据,这样不用进行任何排序操作即可得到满足客户端要求的有序数据返回给客户端; 另外一种则需要通过MySQL 的排序算法将存储引擎中返回的数据进行排序然后再将排序后的数据返回给客户端。 举例说明:如果有两张表:退款表(id,退款id,退款订单号,退款金额),主键为id,普通索引为orde
求帮优化代码....给出建议的给分~~
[code=C/C++]UINT ScanProc(LPVOID lpParam)rnrn CString IP=((IPData*)lpParam)->IP;rn SOCKET sd;rn if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_IP))==INVALID_SOCKET)rn rn ++::g_Complate;rn --::g_ActiveThread;rn return -1;rn rnrn unsigned long flag = 1; rn if ((ioctlsocket(sd, FIONBIO, &flag) != 0))rn rn ++::g_Complate;rn --::g_ActiveThread;rn return -2;rn rn SOCKADDR_IN sin;rn sin.sin_addr.S_un.S_addr=inet_addr(IP);rn sin.sin_family=AF_INET;rn sin.sin_port=htons(::g_Port);rn rn connect(sd,(SOCKADDR*)&sin,sizeof(sin));rn rn rn struct fd_set mask; rn FD_ZERO(&mask);rn FD_SET(sd, &mask);rn rn struct timeval timeout;rn timeout.tv_sec = ::g_TimeOut/1000;rn timeout.tv_usec =::g_TimeOut%1000;rnrn if(select(0, NULL, &mask, NULL, &timeout)<=0)rn rn ++::g_Complate;rn --::g_ActiveThread;rn closesocket(sd);rn return -3;rn rn elsern rn char cmd[513],cmd1[513];rn memset(cmd,0,10);rn memset(cmd1,0,10);rn cmd[0]=5;rn cmd[1]=2;rn cmd[2]=0;rn cmd[3]=2;rnrn if(send(sd,cmd,4,0)!=4)rn rn ++::g_Complate;rn --::g_ActiveThread;rn closesocket(sd);rn return -4;rn rn Sleep(200);rnrn if(recv(sd,cmd1,2,0)!=2)rn rn ++::g_Complate;rn --::g_ActiveThread;rn closesocket(sd);rn return -5;rn rn int index;rn WaitForSingleObject(g_hEvent,INFINITE);rn index=g_pDlg->m_result.GetItemCount();rn g_pDlg->m_result.InsertItem(index,IP);rn SetEvent(g_hEvent);rn g_pDlg->m_result.SetItemText(index,1,::g_strPort);rn switch(cmd1[1])rn rn case 0x02:rn g_pDlg->m_result.SetItemText(index,2,"password");rn break;rn case 0x00:rn g_pDlg->m_result.SetItemText(index,2,"cool");rn break;rn default:rn g_pDlg->m_result.SetItemText(index,2,"unknow");rn break;rn rn rn closesocket(sd);rn ++::g_Complate;rn --::g_ActiveThread;rn return 0;[/code]rnrn以上是一个线程代码~~开启多线程的时候占用较多的CPU,有时候还会出错~~请达人帮忙找一下错误~~
相关热词 c#检测非法字符 c#双屏截图 c#中怎么关闭线程 c# 显示服务器上的图片 api嵌入窗口 c# c# 控制网页 c# encrypt c#微信网页版登录 c# login 居中 c# 考试软件