Archive

Archive for the ‘Code’ Category

java动态代理要用到的反射知识备忘

备忘
import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;

import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class App {
public static void main(String[] args) throws Exception {
System.out.println("start");

String srcString = "public class xxoo { public void f(){ System.out.println(\"I am C\"); } }";

String dir = System.getProperty("user.dir");

String fileName = dir + "\\src\\xxoo.java";
File file = new File(fileName);
FileWriter fWriter = new FileWriter(file);
fWriter.write(srcString);
fWriter.flush();
fWriter.close();

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
System.out.println(compiler.getClass().getName());
StandardJavaFileManager fileManager =
compiler.getStandardFileManager(null, null, null);
Iterable units = fileManager.getJavaFileObjects(fileName);
CompilationTask task = compiler.getTask(null, fileManager, null, null, null, units);
task.call();

URL[] ss = new URL[] { new URL("file://" + dir + "\\src")};
URLClassLoader classLoader = new URLClassLoader(ss);
Class c = classLoader.loadClass("App");

Constructor constructor = c.getConstructor(null);
App app = (App)constructor.newInstance(null);

app.p("shitttt");

java.lang.reflect.Method[] ms = c.getMethods();
for (java.lang.reflect.Method method : ms) {
p(method.getName());
}

System.out.println("over");
}

public static void p(Object o){
System.out.println(o);
}
}

Categories: Code

[译]避免在Joins中出现OR

避免在Joins中出现OR

为什么要避免在joins中出现OR条件,一个优化例子

OR条件和join语句—这两样东西不适合放在一起.类似于or能大大影响带有子查询的查询,他们能给joins语带带来非常坏的影响.

举个例子, 看下面的查询(也许不太符合实际). 这里假设一个数字型表(numbers)已经创建好了 (详见使用笛卡尔积查询方式生成的数据). 假设现在的表里有10,000行记录.

SELECT n1.num, n2.num, n3.num FROM numbers n1
INNER JOIN numbers n2 ON ( (n1.num = n2.num - 400) OR (n1.num = n2.num + 400))
INNER JOIN numbers n3 ON n2.num = n3.num - 300
ORDER BY n1.num, n2.num, n3.num

这个查询看起来非常简单. 然而即使源表只有10,000行记录以及选出的结果只有区区18,900行记录, 却花费了5秒(译者PS:我承认我的机器很差劲, 用了20秒), 而且Management Studio(其实是我的整个机器)在执行的时候失去响应. 这个查询使CPU的使用率飙升到100%. 这个不是个好现象—joins和OR条件根本就不能放在一起(至少是在sql server中是这样).

我遇到这个情况是在BPS(基础程序设计支持).我们有一个学校完成的DTS包用来把学生数据转移到另外一个数据库(采用不同的构架). 其中的一部花费了20分钟, 我只是用它跑了几次. 但因为我最好找更好的方法, 我决定研究一下其中的步骤. 在20分的运行过程中, I fully expected to see some highly complex tangled web of SQL with 30 joins and 150 lines.我不能再犯错误了, 毕竟这只是一个简单的查询(带有or条件的join). 在五分钟内, 我把这条查询拆分成独立的两块, 这样以前需要20分钟的现在只需要2秒.

在上面的查询中, 这只是一中解决方案(使用union)来拆分OR条件. 肯定好友其他实现的方式, 我希望你们在评论中提出其他的解决方案来处理OR条件.

SELECT n1.num, n2.num, n3.num FROM numbers n1
INNER JOIN numbers n2 ON n1.num = n2.num - 400
INNER JOIN numbers n3 ON n2.num = n3.num - 300
UNION
SELECT n1.num, n2.num, n3.num FROM numbers n1
INNER JOIN numbers n2 ON n1.num = n2.num + 400
INNER JOIN numbers n3 ON n2.num = n3.num - 300
ORDER BY n1.num, n2.num, n3.num

使用上面的10,000行记录的表, 这个查询在瞬间完成. 处理800,000行数据的表只需要8秒. 如果我使用第一种查询查询处理那个大的表, 我想的机器不得不重启了.

声明

这篇文章, 包括其中的任何相关的源码和文件,都在Code Project Open License (CPOL) 的许可一下.

原文地址 codeproject

http://www.codeproject.com/Articles/206408/Avoiding-OR-Conditions-with-Joins

 

译者补充: 次日上午, 根据分析, 如果在num加上索引, 速度会提升很多, 果不其然, 完成也是做瞬间.

CREATE CLUSTERED INDEX [_dta_index_numbers_c_9_2041058307__K1] ON [dbo].[numbers] 
(
	[num] ASC
)

原文


Avoiding OR Conditions with Joins

Why OR conditions in join statements should be avoided and an example fix

OR conditions and join statements – some things just don’t go well together. Similar to how OR conditions can significantly impair queries having subqueries, they can wreak havoc on joins as well.

Take, for example, the following [admittedly unrealistic] query. This assumes a numbers table [numbers] has been set up. [See myCartesian join postfor a query to set up a numbers
table.] Assume the numbers table only has 10,000 records.

Collapse

SELECT n1.num, n2.num, n3.num FROM numbers n1
INNER JOIN numbers n2 ON ( (n1.num = n2.num - 400) OR (n1.num = n2.num + 400))
INNER JOIN numbers n3 ON n2.num = n3.num - 300
ORDER BY n1.num, n2.num, n3.num

This query looks simple enough. However, even though the source table only has 10,000 rows and the result only has 18,900 records, it takes 5 seconds to complete, and Management Studio [and
frankly my entire computer] freezes while it is running. This query spiked CPU usage to 100%. That’s not good – joins and OR conditions simply do not mix [at
least on SQL Server].

I encountered this exact type of scenario at BPS. We have a DTS package for bringing over student data from the source location to another database [with a different schema] where the school assignments are done. One of the steps took 20 minutes, and I simply lived with it the first few times it ran. But because it’s best to ask if there’s a better way, I decided to investigate the step. With a 20 minute runtime, I fully expected to see some highly complex tangled web of SQL with 30 joins and 150 lines. I couldn’t have been more wrong – it was a simple query resembling the one above [with an OR
condition in the join]. In under 5 minutes, I’d broken apart the query into two separate steps and what previously took 20 minutes to run only took 2 seconds.

For the query above, here’s one solution [using aUNION] for breaking apart the OR. There are likely other ways and I invite comments for other approaches to removing the OR condition.

Collapse

SELECT n1.num, n2.num, n3.num FROM numbers n1
INNER JOIN numbers n2 ON n1.num = n2.num - 400
INNER JOIN numbers n3 ON n2.num = n3.num - 300
UNION
SELECT n1.num, n2.num, n3.num FROM numbers n1
INNER JOIN numbers n2 ON n1.num = n2.num + 400
INNER JOIN numbers n3 ON n2.num = n3.num - 300
ORDER BY n1.num, n2.num, n3.num

This query runs instantly with a numbers table having 10,000 rows. It finishes in 8 seconds for a table having 800,000 rows. If I had used the larger table with the first query, I’d probably have to reboot.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Categories: database Tags:

关于数学

不算近的一段时间之前, 在工作中遇到了因为数值的频繁运算而引起的结果微小的差异. 这个是在所难免的, 对于非严格环境下的运算, 精度的缩减是可以容许的. 今晚看一本的书的时候巧合看到了一算关于用分数表示小数的方法, 主要是针对那些无限循环的小数. 这个只是个算式而已, 还没到实际的计算机数据结构的程度. 看了一下忍不住动手自己划拉了一下. 真的是生疏了. 合上书这么小小的一页算式却推算不出来.

又要感慨, 工作这么长时间也没有感觉用到那些学到的基础课, 不免又开始质疑自己工作工作价值. 整天面对着那些重复的增删改查\Proc, Trigger, 以及无穷的猜测和实验, 还有付出超人的忍耐力, 工作俨然真的变成了工作.

抛开这段把东西讲迈向工广阔的时间段, 查询文档\调用方法\展示处理数据. 什么字典哈希表排序查找, 现成类库直接调用. 已经很久很久没有很数学有过亲密接触.

这段上下文写了删, 删了写, 意会就行, 不一定要明说.

还有, 用好法律要学好离散数学.

这段时间一直公干在外, 眼看这学弟学妹就要毕业离开了, 也没有机会见他们在学校的最后一面. 同学们, 不知何年何月才能再次相见, 珍重. 很多事情距离是一个巨大的隔阂, 亲人\爱人\朋友间. 虽然已称”地球村”, 但是离我们一般人还是很遥远.

Categories: Code, Sentiment, Work Tags:

debug trigger

June 22, 2011 Comments off

Step one. create a new stored procedure using the code which will trigger the trigger.

Step two. debug the procedure.

Step three. Step through the trigger code using the F11 key or the Step Into button at the statement which could trigger the trigger, such as UPDATE, DELETE or UPDATE.

Step through the trigger until you exit back to the stored procedure, and continue to the end.

PS:  Suppose you can debug a procedure in sql server. 

reference: http://msdn.microsoft.com/en-us/library/ms165046(VS.80).aspx

Categories: database Tags:

数据窗口分组

用pB的数据窗口分组功能, 如果只选择group definition, 对结果的分组操作不同于sql的操作, 是按照选取的顺序遇到不同的分组条件就分组. 如 2,2,3,3,2,2 分组会分成三组.

Categories: PB Tags: , ,

[Repost]PB使用数据存储代替游标

Datastore 是 PwerBuilder中不可视的数据窗口控制。与数据窗口相比除了视觉以外其他各个方面都极其相似。由于datastore具有对数据的交互操作,所以用它来代替在程序中经常使用的游标Cursors. 使用datastore检索数据比游标的速度快,并对数据的分组变得容易,提高比较高级的过滤功能,在程序中不需要变量也可以访问数据,并且在PB中使编码变得相对简单。

使用datastore对数据也有缺点:
使用datastore时候必须有检索对应数据的数据窗口对象;你使用后必须及时的释放datastore占用的内存空间,一面产生内存空洞;

下面举个简单的例子说明如何人在程序中使用游标和datastore进行查询数据,它们具有一曲同工之效:

1,下面是采用游标的方式取得可户信息的事例程序;

String ls_cust_code,ls_customer_name,ls_address

DECLARE lc_my_cursor CURSOR FOR

SELECT customers.cust_id,customers.cust_name,customers.cust_address

FROM customers ORDER BY cust_code;

OPEN lc_my_cursor;

FETCH lc_my_cursor INTO :ls_cust_code,:ls_customer_name,ls_address;

DO WHILE SQLCA.SQLCODE = 0

FETCH lc_my_cursor INTO :ls_cust_code,:ls_customer_name,:ls_address;

LOOP

CLOSE lc_my_cursor

2,采用Datastore实现上面同样的功能:

String ls_cust_code,ls_customer_name,ls_address

long ll_row,ll_row_count

datastore lds_dstastore

lds_datastore = CREATE datastore

lds_datastore.datao b j e c t = "d_customers"

lds_datastore.settranso b j e c t(sqlca)

lds_datastore.retrieve()

ll_row_count = lds_datastore.rowcount()

FOR ll_row = 1 to ll_row_count

ls_cust_code = lds_datastore.getitemstring(ll_row,"cust_id")

ls_cust_code = lds_datastore.getitemstring(ll_row,"cust_name")

ls_cust_code = lds_datastore.getitemstring(ll_row,"cust_address")

NEXT

DESTROY lds_datastore

说明:上面的数据窗口对象包括数据库中Customers表中的cust_id,cust_name,cust_address

Categories: Code Tags: ,

Take a Leap of Faith

Do you want to take a leap of faith or become an old man, filled with regret waiting to die alone?

电影Inception,中文名字是《盗梦空间》,这部电影早就有所耳闻,听说比较难理解,所以找了个比较清静的时间看了一遍。我其实非常少看影视作品的, 尽量减少非授权物品的使用算是其中的一个原因吧,要是人人都这么想,中国的软件行业就不会这么萧条了,又扯远了。多亏是理科出身而且是一个coder,逻辑思考能力还算是不错,前天晚上十点开始看的,两个半小时的电影看到凌晨一点才看完,语言障碍是一个大问题,频繁的暂停去看字幕把时间延长了。看到三分之二的地方我在IM上问候了另一个城市的coder,说我正在看Inception,那人说看过没看懂,在剩余的三层梦境后确实令我犯迷糊了,就像多线程、递归、线程同步、return、break等一样,着实令人眼花缭乱了一把。

在这部影片中,这篇文章的第一句出现了两次,也是我看这部影片印象最深的台词,我是不是现在就在这句台词描述的状态下呢没有奋力一搏,在临死前充满后悔呢?

I need take a leap of faith.

在这篇文章的后面又写下面的这些文字是为了不想让下面的这些文章太显眼,其实下面的这段文字收录到我的日记里面更合适,我还是用英文写吧,免得被一瞟见就能识别出其中的内容。

I have been in this company for more than one year though I have been graduated for for only about  half year. I became a coder when I got in this Co.. I not a boy who could deal with others freely, I be filmily with the computer, there are only two choices in it’s life, 0 and 1. I don’t deceive the computer and can’t neither. But in the real word I have less experiences though I am 24 years old. I want to be go programmer, at this moment at least. But it’s not a good place for me to got this goal because the environment of this company. There is no a integrated team system, I got less experiences of programing since I got in this company. I am a developer and I am always at the place of custom for about one than one month, I am also a implementer. It doesn’t matter for the things above, the worse thins is ……

I think I need to write it in my diary, it’s morn suitably.

Categories: Code, Entertainment Tags: ,